简体   繁体   English

如何在Django的base.html中检查用户是否已通过身份验证?

[英]How can i check if the user is authenticated or not in base.html in django?

If the user anonymous or not, how can i check that in base.html? 如果用户是否匿名,我如何在base.html中进行检查?

Which is correct? 哪个是对的?

{% if request.user.is_authenticated %}

Or 要么

{% if user.is_authenticated %}

How can i pass the variable request.user in base.html? 如何在base.html中传递变量request.user

Try using the Request context: 尝试使用Request上下文:

from django.template import RequestContext

# in a view
return render_to_response('base.html', context_instance = RequestContext(request))

Make sure you take a look at these context processors: https://docs.djangoproject.com/en/dev/ref/templates/api/#django-contrib-auth-context-processors-auth 确保您看一下这些上下文处理器: https : //docs.djangoproject.com/en/dev/ref/templates/api/#django-contrib-auth-context-processors-auth

And then you should be able to access user in your templates 然后您应该可以在模板中访问user

You can use the django shortcut render to always render a template that is automatically passed a RequestContent: 您可以使用django快捷方式render来始终渲染自动传递给RequestContent的模板:

https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#module-django.shortcuts https://docs.djangoproject.com/zh-CN/dev/topics/http/shortcuts/#module-django.shortcuts

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

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