Generic Views Migration describes what class based view replaces what. According to the doc, the only way to pass extra_context is to subclass TemplateView and provide your own get_context_data method. Here is a DirectTemplateView class I came up with that allows for extra_context
as was done with direct_to_template
.
from django.views.generic import TemplateView
class DirectTemplateView(TemplateView):
extra_context = None
def get_context_data(self, **kwargs):
context = super(self.__class__, self).get_context_data(**kwargs)
if self.extra_context is not None:
for key, value in self.extra_context.items():
if callable(value):
context[key] = value()
else:
context[key] = value
return context
Using this class you would replace:
(r'^$', direct_to_template, { 'template': 'index.html', 'extra_context': {
'form': CodeAddForm,
'topStores': get_topStores,
'newsStories': get_dealStories,
'latestCodes': get_latestCode,
'tags':get_topTags,
'bios':get_bios
}}, 'index'),
with:
(r'^$', DirectTemplateView.as_view(template_name='index.html', extra_context={
'form': CodeAddForm,
'topStores': get_topStores,
'newsStories': get_dealStories,
'latestCodes': get_latestCode,
'tags':get_topTags,
'bios':get_bios
}), 'index'),
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…