本文介绍了如何在Django中将get_context_data与TemplateView一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试执行以下操作:
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')
当我尝试访问dahl_books时我的模板是这样的:
When I try to access dahl_books in my template like this:
{% for book in dahl_books %}
dahl_books
在模板上下文中不可用,即使Books QuerySet返回的非零值也是如此。我在模板或 get_context_data
?
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
?
推荐答案
我无法测试它,但是我敢打赌,您需要
I can't test it, but I bet you need
return context
在 get_context_data
的末尾:)
这篇关于如何在Django中将get_context_data与TemplateView一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!