[Twisted-web] Redirect after login with nevow guard

Phil Mayers p.mayers at imperial.ac.uk
Fri Jan 4 11:32:46 EST 2013

On 04/01/13 16:00, Peter Westlake wrote:
> I'm using nevow.guard, and when someone logs in I would like to send
> them to the URL they originally typed. It's possible to do this by
> posting to the URL guard.LOGIN_AVATAR + other stuff, e.g. to go to
> http://example.com/foo/bar after logging in, the login page should post
> to http://example.com/__login__/foo/bar. This works nicely. The problem
> is, the login page doesn't know that the user was trying to go to
> foo/bar. How can it get that information? I would have thought that it
> would be available as the segments argument in locateChild, but for some
> reason it isn't. Before logging in, the segments argument is always
> empty.

Looking at some *very* old code, we have something like this:

class LoginPage(rend.Page):

     def locateChild(self, c, s):
         return self, []

     def data_misc(self, ctx, data):
         u = url.root.child(guard.LOGIN_AVATAR)
         for path in inevow.ICurrentSegments(ctx):
             u = u.child(path)
         return {
             'action': u

     docFactory = loaders.stan(
             T.title['ICT Host Database - not logged in'],
         ], # head
         T.body(data=T.directive('misc'), render=T.directive('mapping'))[
           T.form(action=T.slot('action'), method='post')[

components.registerAdapter(LoginPage, ihdb.IAnonymous, inevow.IResource)

i.e. the component you register as the adapter for the "Anonymous" user 
needs a "data" method that gathers the url segments, and you reference 
that data method from the "action" value of the login form - so the form 
will be posted to:


More information about the Twisted-web mailing list