# views.py
from django.views.generic import ListView
from books.models import Publisher
class PublisherList(ListView):
model = Publisher
context_object_name = 'my_favorite_publishers'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['number'] = random.randrange(1, 100)
return context
What does calling get_context_data with super() return?
What type of information?
And is the returned context from get_context_data given the contexT_object_name 'my_favorite_publishers'?
The .get_context_data(..)
method [Django-doc] returns a dictionary that contains the context that will be passed to the template for rendering.
A ListView
[Django-doc] will by default make a dictionary with the following keys and values:
'view'
: maps to the instance of this view; 'paginator'
: the paginator object if you paginate, None
otherwise; 'page_obj'
: the page object of the current page if you paginate, None
otherwise; 'is_paginated'
: True
if you paginate, False
otherwise; 'object_list'
: the (optionally) paginated queryset that is made by the ListView
; andcontext_object_name
: if you specified a context_object_name
(or you have overwritten get_context_object_name
and it does not return None
, it will associate this with the (optionally) paginated queryset as well. class EmployeeDetailView(DetailView):
queryset=Employee.objects.all()
template_name='testapp/detail.html'
def get_context_data(self,**kwargs):
context=super().get_context_data(**kwargs)
return context
In the above example,the class EmployeeDetailView is looking for an object(default context for DetailView) this object is provided by calling the method present in parent class that is get_context_data this method accepts kwargs as parameter.
kwargs is a variable length argument list.the function get_context_data returns the context that contains object and this object would be sent to detail.html file,to display the data that the current objects holds(rendering process)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.