How do you use get_context_data with TemplateView in Django [closed]

二次信任 提交于 2019-12-03 04:44:02

问题


I'm trying to do something like this:

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')

When I try to access dahl_books in my template like this:

{% for book in dahl_books %}

dahl_books is not available in the template context, even though the Books QuerySet returned a non-zero number of books. ....am I doing something wrong in either my template or in get_context_data?


回答1:


I can't test it, but I bet you need

return context

at the end of get_context_data :)



来源:https://stackoverflow.com/questions/8031890/how-do-you-use-get-context-data-with-templateview-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!