What is context_object_name in django views?

megido picture megido · May 11, 2011 · Viewed 23.1k times · Source

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?

Answer

user2395922 picture user2395922 · Nov 20, 2013

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