44

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

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什么?

4

1 回答 1

67

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

return context

在结束时get_context_data:)

于 2011-11-07T01:35:46.057 回答