簡體   English   中英

如何在Django的base.html中檢查用戶是否已通過身份驗證?

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

如果用戶是否匿名,我如何在base.html中進行檢查?

哪個是對的?

{% if request.user.is_authenticated %}

要么

{% if user.is_authenticated %}

如何在base.html中傳遞變量request.user

嘗試使用Request上下文:

from django.template import RequestContext

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

確保您看一下這些上下文處理器: https : //docs.djangoproject.com/en/dev/ref/templates/api/#django-contrib-auth-context-processors-auth

然后您應該可以在模板中訪問user

您可以使用django快捷方式render來始終渲染自動傳遞給RequestContent的模板:

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