web2py 中的登录后重定向 [英] Post-login redirection in web2py

查看:28
本文介绍了web2py 中的登录后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法控制 web2py 中的登录后重定向行为.

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

根据this,web2py 以不同的方式处理登录后重定向,具体取决于登录是由系统发起的(例如,访问受身份验证保护的功能时)还是由用户发起的(点击登录"链接时).在前一种情况下,行为是在登录后重定向到引用页面,正如预期的那样.但是,在后者中,用户在登录后会被重定向到索引页面,或者被重定向到 auth.settings.login_next 中硬编码的页面.

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?

推荐答案

您可以更改登录"链接,使它们始终包含当前 URL 作为查询字符串中 _next 变量的值.例如,无论您在何处创建登录链接,都可以这样定义:

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

这会将当前页面的 URL(包括任何 args 和 vars)添加到登录链接的查询字符串中的 _next 变量,这将导致重定向回当前页面登录后.

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.

如果您使用 auth.navbar() 帮助程序生成您的登录链接,则主干中已经有解决此问题的更改(即将发布).新的 auth.navbar() 将自动将 _next 变量添加到所有链接,因此在单击任何导航栏链接后,用户将被重定向回原始页面.同时,您可以按如下方式编辑 auth.navbar():

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

这篇关于web2py 中的登录后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆