You might have noticed that our sample publisher list template stores all the publishers in a variable named object_list
. While this works just fine, it isn't all that "friendly" to template authors: they have to "just know" that they're dealing with publishers here.
In Django, if you're dealing with a model object, this is already done for you. When you are dealing with an object or queryset, Django populates the context using the lower cased version of the model class' name. This is provided in addition to the default object_list
entry, but contains exactly the same data, that is publisher_list
.
If this still isn't a good match, you can manually set the name of the context variable. The context_object_name
attribute on a generic view specifies the context variable to use:
# views.py
from django.views.generic import ListView
from books.models import Publisher
class PublisherList(ListView):
model = Publisher
context_object_name...