Python 如何在django中的base.html中检查用户是否经过身份验证?

Python 如何在django中的base.html中检查用户是否经过身份验证?,python,django,templates,authentication,Python,Django,Templates,Authentication,如果用户是否匿名,如何在base.html中检查 哪个是正确的 {% if request.user.is_authenticated %} 或 如何在base.html中传递变量request.user?尝试使用请求上下文: from django.template import RequestContext # in a view return render_to_response('base.html', context_instance = RequestContext(reques

如果用户是否匿名,如何在base.html中检查

哪个是正确的

{% if request.user.is_authenticated %}


如何在base.html中传递变量
request.user

尝试使用请求上下文:

from django.template import RequestContext

# in a view
return render_to_response('base.html', context_instance = RequestContext(request))
请确保查看以下上下文处理器:

然后您应该能够访问模板中的
user

您可以使用django快捷方式
render
始终呈现自动传递RequestContent的模板:


尝试使用请求上下文:

from django.template import RequestContext

# in a view
return render_to_response('base.html', context_instance = RequestContext(request))
请确保查看以下上下文处理器:

然后您应该能够访问模板中的
user

您可以使用django快捷方式
render
始终呈现自动传递RequestContent的模板:


所有模板都扩展了
base.html
,您没有在那里传递任何上下文。所以我必须以这种方式传入每个视图方法吗<代码>返回render_to_响应('base.html',{'user':request.user,context_实例=RequestContext(request))在每个视图方法中传递
request.user
不是一个好主意。您不需要显式地在中传递用户对象。它将通过传递RequestContext来提供。您可以在需要将请求传递到模板中的视图中执行此操作。
base.html
对于整个项目来说是通用的,在
base.html
中>它需要检查它是否是登录用户,根据这一点,它将打印登录或注销锚链接。请参阅我的最新更新以使用
呈现
快捷方式。是的,您的响应需要始终包含RequestContext,默认情况下会这样做。所有模板都扩展了
base.html
,您没有在那里传递任何上下文。那么我必须以这种方式传入每个视图方法吗?
返回render_to_response('base.html',{'user':request.user,context_instance=RequestContext(request))
在每个视图方法中传递
request.user
不是一个好主意。您不需要显式地在中传递用户对象。它将通过传递RequestContext来提供。您可以在需要将请求传递到模板中的视图中执行此操作。
base.html
对于整个项目来说是通用的,在
base.html
中>它需要检查是否是登录用户,根据这一点,它将打印登录或注销锚链接。请参阅我的最新更新以使用
render
快捷方式。是的,您的响应需要始终包含RequestContext,默认情况下会这样做。