[英]How to know which page redirected to the login URL with the @login_required decorator in Django?
[英]Django - how to tell if user was redirected to the page by @login_required?
我想用
@login_required
视图的装饰器
allUsers_page
,当您转到url时使用
r'^users/allUsers$'
在我的settings.py中,我有
LOGIN_URL='/login'
因此,当用户转到
allUser_page
查看(当他转到网址时
users/allUsers
,如果用户未登录其帐户,则会将其重定向到登录页面。 现在,在登录页面中,有一种方法可以让我找出用户是否只是直接进入了登录页面,还是用户被用户重定向到登录页面。
@login_required
装饰员? 基本上,我希望我的登录页面类似于
{% if the user was redirected to this page by @login_required %}
<p>Hi, you must log in in order to view the page you were trying to view.</p>
{% else %}
<p>Welcome! Please sign in.</p>
<!-- Here is where the log in form is -->
注意:我使用的是通用登录视图。
如果从另一个页面重定向了该用户,则django将提供next
查询字符串参数,以便在登录后可以将该用户重定向回。
将此添加到您的settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
)
然后,在模板中,检查此参数是否存在。
{% if 'next' in request.GET %}
不过,未经测试。
请参阅验证文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.