开发者

What is context_object_name in django views?

开发者 https://www.devze.com 2023-03-05 12:20 出处:网络
I\'m new to django. And now I\'m studying using the class-based generic views. Could someone plea开发者_运维技巧se explain the aim and use of context_object_name attribute?If you do not provide \"cont

I'm new to django. And now I'm studying using the class-based generic views. Could someone plea开发者_运维技巧se explain the aim and use of context_object_name attribute?


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


Ok, I've got it by myself! :)

It's just a human-understandable name of variable to access from templates

https://docs.djangoproject.com/en/1.10/topics/class-based-views/generic-display/#making-friendly-template-contexts


Lets assume the following posts/views.py:

# posts/views.py
from django.views.generic import ListView from .models import Post

class HomePageView(ListView): 
    model = Post
    template_name = 'home.html'

On the first line we’re importing ListView and in the second line we need to explicitly define which model we’re using. In the view, we subclass ListView, specify our model name and specify our template reference. Internally ListView returns an object called object_list that we want to display in our template.

In our templates file home.html we can use the Django Templating Language’s for loop to list all the objects in object_list

Why object_list? This is the name of the variable that ListView returns to us.

Lets look at our templates/home.html

<!-- templates/home.html -->
<h1>Message board homepage</h1> 
<ul>

     {% for post in object_list %} 

          <li>{{ post }}</li>

     {% endfor %} 
 </ul>

You see the object_list above? It is not a very friendly name? To make it more user-friendly, we can provide instead an explicit name using context_object_name.

This helps anyone else reading the code to understand what is variable in the template context, plus it is much easier to read and understand.

So let's go back to our posts/views.py and change it by adding the one line below:


context_object_name = 'all_posts_list' # <----- new

So our new views.py looks like this now:

# posts/views.py
from django.views.generic import ListView from .models import Post

class HomePageView(ListView): model = Post

    template_name = 'home.html'

    context_object_name = 'all_posts_list' # <----- new

And let's not forget to update our template now:

<!-- templates/home.html -->
<h1>Message board homepage</h1> 
<ul>

   {% for post in all_posts_list %}

      <li>{{ post }}</li>

   {% endfor %} 

</ul>

You could have left as object_list and it would still work, but you get the idea.


Consider these 2 code snippet

A. Using function based view:

def index(request):
    product_list = Product.objects.all()
    return render(request, 'product/index.html', {'product_list': **product_list**})

B. Using class based view

class ProductListView(ListView):
    model = Product
    template_name = 'product/index.html'
    context_object_name = 'product_list'

In both the above method your context variable will be "product_list", and your HTML will be,

{% for product in product_list %}
<div class="row">
  <div class="col-md-3 offset-md-2">
    <img src="{{product.product_image}}" class="card" height="150px" />
  </div>
  <div class="col-md-4">
    <h3>{{product.product_name}}</h3>
   .......
  </div>
  <div class="col-md-2">
  .........  
  </div>
</div>
{% endfor %}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号