所以Django 1.5不再使用direct_to_template调用。
我移植了一些网址,我被此拦住了。
url(r'^system_settings/$',
'django.views.generic.simple.direct_to_template',
{
'template': 'helpdesk/system_settings.html',
'extra_context': {
'ADMIN_URL': getattr(settings, 'ADMIN_URL', '/admin/'),
},
},
我知道我可以使用
url(r'^help/context/$',
TemplateView.as_view(template_name='helpdesk/system_settings.html')...
但我不知道extra_context
应该去哪里。有什么见解吗?
答案 0 :(得分:5)
参考:https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-display/#adding-extra-context
from django.views.generic import TemplateView
from django.utils.decorators import classonlymethod
utils.autodiscover()
class ExtraContextTemplateView(TemplateView):
@classonlymethod
def get_context_data(self, **kwargs):
context = super(ExtraContextTemplateView, self).get_context_data(**kwargs)
context['ADMIN_URL'] = getattr(settings, 'ADMIN_URL', '/admin/')
return context
urlpatterns = patterns('',
url(r'^help/context/$', ExtraContextTemplateView.as_view(template_name='helpdesk/system_settings.html')
)
答案 1 :(得分:1)
修改上述示例以支持传递值字典
class ExtraContextTemplateView(TemplateView):
extra_context = {}
def get_context_data(self, **kwargs):
context = super(ExtraContextTemplateView, self).get_context_data(**kwargs)
context.update(extra_context)
return context
urlpatterns = patterns('',
url(r'^help/context/$',
ExtraContextTemplateView.as_view(template_name='helpdesk/system_settings.html',
extra_context={'title': 'Help-System Settings'})
答案 2 :(得分:1)
修改Shah Shah的回答,(我没有必要的50个声誉来简单评论他的帖子):
我认为您需要更改行
context.update(extra_context)
到
context.update(self.extra_context)
因为extra_context是一个类属性。