4

I'm having trouble controlling post-login redirection behavior in web2py.

According to this, web2py handles post-login redirection differently, depending on whether the login was initiated by the system (e.g. when accessing a auth-protected function) or by a user (when clicking a 'log in' link). In the former case, the behavior is to redirect to the referring page after login, as would be expected. In the latter, however, the user is redirected to an index page after login, or to a page hardcoded in auth.settings.login_next.

How would I set things up so that post-login redirection always returns you to the referring page, regardless of how the login was initiated?

1 Answer 1

5

You can change your "Login" links so they always include the current URL as the value of the _next variable in the query string. For example, wherever you create a login link, define it like this:

A('Login', _href=URL('default', 'user', args='login',
    vars=dict(_next=URL(args=request.args, vars=request.vars))))

That will add the URL of the current page (including any args and vars) to the _next variable in the query string of the login link, which will result in a redirect back to the current page after login.

If you are using the auth.navbar() helper to generate your login link, there is already a change in trunk that addresses this issue (will be released very soon). The new auth.navbar() will automatically add the _next variable to all the links, so after clicking any navbar link, the user is redirected back to the original page. In the meantime, you can edit auth.navbar() as follows:

import urllib
navbar = auth.navbar()
if not auth.is_logged_in and request.args(0) != 'login':
    navbar[1]['_href'] += '?_next=' +\
        urllib.quote(URL(args=request.args, vars=request.vars))
3
  • 1
    @Anthony- thanks a lot, this works. And yes I definitely think this should be should be the default behavior in any case. The current inconsistent behavior is confusing and undocumented.
    – Yarin
    Dec 2, 2011 at 17:33
  • if I put this in my model for usersdatatabe, I get the following error AttributeError: 'NoneType' object has no attribute 'fields'
    – Yebach
    Aug 30, 2012 at 10:16
  • found a solution. auth.settings.logout_next was working but my url was not ok or was incomplete so I was redirected to default/index page
    – Yebach
    Aug 30, 2012 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.