嗨:)我的模板双扩展系统有一个小问题。我有一个计划:
base.html ---> index.html ---> something.html
当我登录网站时,我可以访问所有不可见的块(匿名用户的隐形块),如:
{% if user.is_superuser %}
blabla
{% endif %}
所以“blabla”对我来说是显而易见的,因为我是超级用户并且我已登录。它在base.html,index.html中工作正常,但没有在something.html中工作。为什么??简单看起来像用户:'超级用户'已注销。
答案 0 :(得分:1)
您是否将请求context传递给render_to_response(或HttpResponse)?
有关登录用户的信息必须存储在上下文(see documentation)中,您必须明确地执行此操作。
通用视图会自动执行此操作,但如果您使用自己的视图查看something.html,并直接调用render_to_response,则表示您没有关于该用户的信息。
因此,视图中的代码应如下所示:
from django.shortcuts import render_to_response
from django.template import RequestContext
def my_personalized_view(request):
return render_to_response('something.html',
{},
context_instance=RequestContext(request))