Post-login redirection in web2py

馋奶兔 提交于 2020-01-14 13:10:25

问题


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:


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))


来源:https://stackoverflow.com/questions/8359263/post-login-redirection-in-web2py

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!