Django - 检查用户是否已被重定向到当前页面

时间:2014-02-17 19:57:28

标签: django validation redirect get request

我正在使用Django 1.5,我想检查用户是否已被重定向到我的登录页面,因为他试图访问另一个需要登录但未登录的页面。假设有一个名为'securityPage'的视图并假设当我访问

时调用该视图
/securityInfo/

URL。假设这是securityPage视图:

@login_required
def securityPage(request):
    #some code

现在当我访问该网址时,它会将我重定向到login.html页面,这是正确的(因为用户 - 我 - 没有登录任何帐户)。这是我的login.html:

{% if 'next' in request.GET %}
    <p>You must first login into your account before having access to the page you were trying to view.</p>
{% endif %}

出于某种原因,即使我被重定向并且我被重定向到的登录页面的URL是

http://127.0.0.1:8000/login/?next=/securityInfo/

,行

{% if 'next' in request.GET %}
即使URL中的“下一个”,

也会计算为false。知道为什么吗?

1 个答案:

答案 0 :(得分:1)

我认为请求对象不是默认上下文值的一部分。

检查设置中的template_context_processors,如果在该列表中添加 django.core.context_processors.request ,则当前请求将添加到每个requestcontext。或者只是手动传递该视图的请求。

https://docs.djangoproject.com/en/1.5/ref/templates/api/#django-core-context-processors-request

相关问题