【发布时间】:2012-04-27 02:56:42
【问题描述】:
我的 urls.py 中有这条路径:
archive_index_dict = {
'queryset': News.objects.filter(show=True),
'date_field': 'date',
'template_object_name': 'object_list',
}
...
url(r'^$', 'django.views.generic.date_based.archive_index',
archive_index_dict, name='news_archive_index'
),
现在我想在模板中检测页面是否为当前页面(这是用于菜单样式)。 {{ request.path }} 和 {{ request.get_full_path }} 在模板中都不起作用。
我应该改用什么?
解决方案
要在模板中获得request,我必须将django.core.context_processors.request 添加到TEMPLATE_CONTEXT_PROCESSORS。这不是默认设置的(从 django 1.3 开始)。
【问题讨论】:
-
您是否设置了
django....request上下文处理器?几乎所有 CBV 默认使用RequestContext -
天啊,当然不是! :) 谢谢你的提示,我已经在设置中添加了
'django.core.context_processors.request',request现在可以在模板中使用。顺便说一句,你可以发表你的评论作为答案,我会投票给它。
标签: django django-generic-views requestcontext django-class-based-views