django 视图中的 context_object_name 是什么?

发布于 2024-11-06 12:26:51 字数 96 浏览 1 评论 0 原文

我是 django 的新手。现在我正在学习使用基于类的通用视图。 有人可以解释一下 context_object_name 属性的目的和用途吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绳情 2024-11-13 12:26:52

如果您不提供“context_object_name”,您的视图可能如下所示:

<ul>
    {% for publisher in object_list %}
        <li>{{ publisher.name }}</li>
    {% endfor %}
</ul>

但如果您提供像{“context_object_name”:“publisher_list”},那么您可以编写视图如下:

<ul>
    {% for publisher in publisher_list %}
        <li>{{ publisher.name }}</li>
    {% endfor %}
</ul>

这意味着您可以更改原始参数名称(object_list)通过“context_object_name”进入任何名称以供您查看。
希望有帮助:)

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

永不分离 2024-11-13 12:26:52

好吧,我自己已经搞定了! :)

这只是一个人类可以理解的变量名称,可以从模板访问

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

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

稳稳的幸福 2024-11-13 12:26:52

让我们假设以下 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'

在第一行中我们导入 ListView,在第二行中我们需要显式定义我们正在使用的模型。在视图中,我们对 ListView 进行子类化,指定模型名称并指定模板引用。 ListView 在内部返回一个名为 object_list 的对象,我们希望在模板中显示该对象。

在我们的模板文件 home.html 中,我们可以使用 Django 模板语言的 for 循环来列出 object_list 中的所有对象

为什么是 object_list? 这是 ListView 返回的变量名称对我们来说。

让我们看看我们的 templates/home.html

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

     {% for post in object_list %} 

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

     {% endfor %} 
 </ul>

你看到上面的 object_list 了吗?是不是一个很亲切的名字?
为了使其更加用户友好,我们可以使用 context_object_name 提供显式名称。

这有助于其他阅读代码的人了解模板上下文中的变量是什么,而且更容易阅读和理解。

因此,让我们回到 posts/views.py 并通过添加下面一行来更改它:


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

所以我们的新views.py 现在看起来像这样:

# 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

现在我们不要忘记更新我们的模板:

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

   {% for post in all_posts_list %}

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

   {% endfor %} 

</ul>

您可以保留为 object_list ,它仍然有效,但你明白了。

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.

放飞的风筝 2024-11-13 12:26:52

考虑这 2 个代码片段

A. 使用基于函数的视图:

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

B. 使用基于类的视图

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

在上述两种方法中,您的上下文变量将是“product_list”,您的 HTML 将是,

{% 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 %}

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 %}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文