如何在基于类的通用视图中访问RequestContext? [英] How to access RequestContext in class-based generic views?

查看:220
本文介绍了如何在基于类的通用视图中访问RequestContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的urls.py中有这条路径:

  archive_index_dict = {
'queryset':新闻。 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'
),

现在我想在模板中检测到一个页面是否为最新(这是菜单样式)。 c $ {$ {code> {{request.path}} {{request.get_full_path}}

我应该用什么?



解决方案



要在模板中获取请求,我不得不将 django.core.context_processors.request 添加到 TEMPLATE_CONTEXT_PROCESSORS 。这不是默认设置的(因为django 1.3)。

解决方案

你有没有'django.core .context_processors.request'上下文处理器设置?几乎所有CBV使用 RequestContext 默认情况下


I have this path in my 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'
    ),

Now I want to detect in template if a page is current (this is for menu styling). Neither {{ request.path }} nor {{ request.get_full_path }} work in template.

What should I use instead?

SOLUTION

To get request available in templates I had to add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS. This is not set by default (since django 1.3).

解决方案

Do you have 'django.core.context_processors.request' context processor set up? Almost all CBV use RequestContext by default

这篇关于如何在基于类的通用视图中访问RequestContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆