I'm new to django. And now I'm studying using the class-based generic views. Could someone please explain the aim and use of context_object_name attribute?
Django has two types of views; function-based views (FBVs), and class-based views (CBVs).
Unlike classic views, generic views are classes not functions. Django offers a set of classes for generic views in django. views. generic, and every generic view is one of those classes or a class that inherits from one of them.
Django's generic views were developed to ease that pain. They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to write too much code.
CreateView. A view that displays a form for creating an object, redisplaying the form with validation errors (if there are any) and saving the object. This view inherits methods and attributes from the following views: django.
If you do not provide "context_object_name", your view may look like this:
<ul> {% for publisher in object_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul>
But if you provide like {"context_object_name": "publisher_list"}, then you can write view like:
<ul> {% for publisher in publisher_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul>
That means you can change the original parameter name(object_list) into any name through "context_object_name" for your view. Hope that help:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With