'is_authenticated'仍然总是在模板中返回false [英] 'is_authenticated' still always returns false in template
问题描述
以下是我的其他问题,因为它可能需要不同的答案。
我的模板中的代码总是返回false:
{%if request.user.is_authenticated%}
< p>我登录了< / p>
{%else%}
< p>我是匿名的< / p>
{%endif%}
我从文档中了解到,我需要包括django .contrib.auth.context_processors.auth在settings.py中的 TEMPLATE_CONTEXT_PROCESSORS
中,所以我已经这样做了,如下所示:
TEMPLATE_CONTEXT_PROCESSORS =(
/ pre>
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.contrib.auth.context_processors.auth,#Added
)
对于我的views.py,我使用
render_to_response()
,但感觉这可能是导致问题。我还需要在上下文中从数据库传递一个用户,但添加了第三个参数context_instance = RequestContext(request)
def public_profile(request,username_in_url):
user = User.objects.get(username = username_in_url)
context = {
'user ':user,
}
return render_to_response('public_profile.html',context,context_instance = RequestContext(request))
为什么
request.user.is_authenticated
仍然返回false,即使用户登录?在views.py中使用相同的方法正常工作 。
如果request.user.is_authenticated():
返回render_to_response('public_profile.html',上下文)
else:
return render_to_response('public_profile_anon.html',context)
解决方案django.contrib.auth.context_processors.auth 说,这个上下文处理器定义了两个变量:
user
和perms
。而用户
是
一个
auth.User
实例,表示当前登录的用户(或$ code> AnonymousUser
实例,如果客户端未登录)。
所以尽管在您的Python代码中,您可以访问
request.user
来执行检查,在模板中用户
。
您的具体案例的问题是您还会传递一个基于<$ code> user 的字典C $ C> username_in_url 。这与由
django.contrib.auth.context_processors.auth
定义的变量冲突。
所以:
更改字典(你命名为
context
),以便用户
具有不同的名称。 (并更改您的模板中的引用。)
更改模板以检查
{%if user.is_authenticated%}
。
This is following on from my other question, as it will likely require a different answer.
The code in my template is always returning false:
{% if request.user.is_authenticated %} <p>I'm logged in</p> {% else %} <p>I am anonymous</p> {% endif %}
I understand from the docs that I need to include "django.contrib.auth.context_processors.auth" within
TEMPLATE_CONTEXT_PROCESSORS
in settings.py so I have done so as follows: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.contrib.auth.context_processors.auth", #Added )
As for my views.py, I am using the
render_to_response()
but feel this may be causing the issue. I also need to pass a user from the database within the context, but have added the third parametercontext_instance=RequestContext(request)
def public_profile(request, username_in_url): user = User.objects.get(username = username_in_url) context = { 'user': user, } return render_to_response('public_profile.html', context, context_instance=RequestContext(request))
Why does
request.user.is_authenticated
still return false even though a user is logged in? Using the same method within views.py works correctly e.g.if request.user.is_authenticated(): return render_to_response('public_profile.html', context) else: return render_to_response('public_profile_anon.html', context)
解决方案The documentation for
django.contrib.auth.context_processors.auth
says that this context processor defines two variables:user
andperms
. Anduser
isAn
auth.User
instance representing the currently logged-in user (or anAnonymousUser
instance, if the client isn’t logged in).So although in your Python code you would access
request.user
to perform your check, in the template it isuser
.The problem with your specific case is that you also pass a dictionary that defines
user
on the basis ofusername_in_url
. This clashes with the variable defined bydjango.contrib.auth.context_processors.auth
.So:
Change the dictionary (which you named
context
) so thatuser
has a different name. (And change references to it in your template.)Change your template to check
{% if user.is_authenticated %}
.
这篇关于'is_authenticated'仍然总是在模板中返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!