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.