Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is context_object_name in django views?

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?

like image 556
megido Avatar asked May 11 '11 05:05

megido


People also ask

What are the types of views in Django?

Django has two types of views; function-based views (FBVs), and class-based views (CBVs).

What is generic views in Django?

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.

Why use Django generic views?

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.

What does CreateView do in Django?

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.


1 Answers

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:)

like image 130
user2395922 Avatar answered Sep 22 '22 19:09

user2395922