简体   繁体   English

Django-如何判断@login_required是否将用户重定向到页面?

[英]Django - how to tell if user was redirected to the page by @login_required?

I want to use the 我想用

@login_required 

decorator for a view called 视图的装饰器

allUsers_page

, which is used when you go to the url ,当您转到url时使用

r'^users/allUsers$'

In my settings.py, I have 在我的settings.py中,我有

LOGIN_URL='/login'

so when a user goes to the 因此,当用户转到

allUser_page

view (when he goes to the url 查看(当他转到网址时

users/allUsers

, if the user is not signed into his account, it will redirect him to the login page. ,如果用户未登录其帐户,则会将其重定向到登录页面。 Now, in the login page, is there a way for me to find out if the user just went to the login page directly or if he was redirected to the login page by the 现在,在登录页面中,有一种方法可以让我找出用户是否只是直接进入了登录页面,还是用户被用户重定向到登录页面。

@login_required

decorator? 装饰员? Basically, I want my login page to be something like 基本上,我希望我的登录页面类似于

{% 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 -->

NOTE: I am using the generic login view. 注意:我使用的是通用登录视图。

If the user user was redirected from another page, django will provide the next query string parameter, so that the user can be redirected back after signing in. 如果从另一个页面重定向了该用户,则django将提供next查询字符串参数,以便在登录后可以将该用户重定向回。

Add this to your settings.py 将此添加到您的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",
)

then, in the template, check if this parameter exists or not. 然后,在模板中,检查此参数是否存在。

{% if 'next' in request.GET %}

Not tested, though. 不过,未经测试。

See Auth docs 请参阅验证文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM