Making "friendly" template contexts

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

Get Mastering Django: Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.