Reputation: 1127
Im following Mozilla Django Tutorial and created following view:
view.py
class BookListView(generic.ListView):
model = Book
queryset = Book.objects.filter(title__icontains='war')[:5] # Get 5 books containing the title war
context_object_name = 'book_list' # your own name for the list as a template variable
template_name = 'catalog/index.html' # Specify your own template name/location
paginate_by = 3
I have reorganized files in my project and I want to display additional information on my template. I tried to do that using get_context_data(self, **kwargs):
but then I get only that what I have defined inside this method.
Changed to:
class BookListView(generic.ListView):
model = Book
#queryset = Book.objects.filter(title__icontains='war')[:5] # Get 5 books containing the title war
#context_object_name = 'book_list' # your own name for the list as a template variable
template_name = 'catalog/index.html' # Specify your own template name/location
paginate_by = 3
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(BookListView, self).get_context_data(**kwargs)
display_books = Book.objects.filter(title__icontains='war')[:5]
# Generate counts of some of the main objects
num_books=Book.objects.all().count()
num_instances=BookInstance.objects.all().count()
# Available books (status = 'a')
num_instances_available=BookInstance.objects.filter(
status__exact='a').count()
num_authors=Author.objects.count() # The 'all()' is implied by default.
# Create any data and add it to the context
context = {
'display_books':display_books,
'num_books':num_books,
'num_instances':num_instances,
'num_instances_available':num_instances_available,
'num_authors':num_authors
}
return context
This way is my pagination doesn't work.
Upvotes: 0
Views: 179
Reputation: 2061
When you do :
context = super(BookListView, self).get_context_data(**kwargs)
You get the context, but you re-assign context at the end :
context = {
'display_books':display_books,
'num_books':num_books,
'num_instances':num_instances,
'num_instances_available':num_instances_available,
'num_authors':num_authors
}
what you need to do is add element to the dict:
context["display_books"] = display_books
context["num_books"] = num_books
context["num_instances"] = num_instances
context["num_instances_available"] = num_instances_available
context["num_authors"] = num_authors
Upvotes: 0
Reputation: 308779
The problem is that you replace the context
dict from the super()
call with a brand new dictionary.
def get_context_data(self, **kwargs):
context = super(BookListView, self).get_context_data(**kwargs)
...
context = {
'display_books':display_books,
'num_books':num_books,
'num_instances':num_instances,
'num_instances_available':num_instances_available,
'num_authors':num_authors
}
return context
You should update the existing dictionary instead:
def get_context_data(self, **kwargs):
context = super(BookListView, self).get_context_data(**kwargs)
...
context['display_books'] = display_books
context['num_books'] = num_books
context['num_instances'] = num_instances
context['num_instances_available'] = num_instances_available
context['num_authors'] = num_authors
return context
Upvotes: 1