如何在Django中使用带有TemplateView的get_context_data

时间:2011-11-07 01:33:45

标签: django django-templates django-views django-class-based-views

我正在尝试做这样的事情:

class AboutView(TemplateView):
    template_name = 'about.html'

    def get_context_data(self, **kwargs):
        context = super(AboutView, self).get_context_data(**kwargs)
        context['dahl_books'] = Books.objects.filter(author="Dahl')

当我尝试在我的模板中访问dahl_books时:

{% for book in dahl_books %}

dahl_books在模板上下文中不可用,即使Books QuerySet返回了非零数量的书籍。 ....我在模板或get_context_data中做错了什么?

1 个答案:

答案 0 :(得分:54)

我无法测试,但我打赌你需要

return context

get_context_data:)

的末尾