Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 781
9-bits Avatar asked Nov 07 '11 01:11

9-bits


People also ask

When to use TemplateView in Django?

TemplateView should be used when you want to present some information in a html page. TemplateView shouldn't be used when your page has forms and does creation or update of objects. In such cases FormView, CreateView or UpdateView is a better option.

What is TemplateView Django?

Django Templates are used to create HTML interfaces that get rendered with a Django view. A TemplateView is a generic class-based view that helps developers create a view for a specific template without re-inventing the wheel. TemplateView is the simplest one of many generic views provided by Django.


1 Answers

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

return context 

at the end of get_context_data :)

like image 159
Jakub Gocławski Avatar answered Oct 16 '22 11:10

Jakub Gocławski