FAQ

Modules

When loading a Nevow application using built-in Twisted server via "twistd -noy file.tac", why do I get an import error when importing my module "foo"?

Twisted does not recognize the current directory as loaded in Python path. On your file.tac, before any nevow or twisted modules are imported do this :

# file.tac
import os, sys
sys.path.append(os.getcwd()) # loads the current directory to Python path

# then start loading nevow and twisted modules
from twisted.application import service, internet

# start importing your modules here
from foo import BarPage
# etc...
-----------------------
# foo.BarPage.py

from nevow.rend import Page
class BarPage( Page ):
    def renderHTTP( self, ctx ):
        return "foo"

Request parameters

How do I extract request parameters

Request parameters come from the context object (but beware this: ). You adapt the context object to an IRequest object. The parameters are stored in the args dictionary.

request = inevow.IRequest(ctx)
if request.args.has_key('myparam'):
   validate(request.args['myparam'])

Testing

How do I make fake context objects?

Divmod contains a testutil module that contains a set of ready made classes like FakeRequest, FakeSession etc. Use these. Example:

ctx = context.WebContext(tag=page)
ctx.remember(testutil.AccumulatingFakeRequest())
jethro@divmod.org