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?
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: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 newauth.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 editauth.navbar()
as follows: