【问题标题】:What is context_object_name in django views?Django 视图中的 context_object_name 是什么?
【发布时间】:2022-11-29 22:42:24
【问题描述】:

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

【问题讨论】:

    标签: django-class-based-views


    【解决方案1】:

    如果您不提供“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>
    

    这意味着您可以通过视图的“context_object_name”将原始参数名称(object_list)更改为任何名称。 希望有所帮助:)

    【讨论】:

      【解决方案2】:

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

      它只是从模板访问的变量的人类可理解的名称

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

      【讨论】:

      【解决方案3】:

      让我们假设以下 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 返回一个名为对象列表我们想要在我们的模板中显示。

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

      为什么是 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 了吗?是不是很亲切的名字? 为了使它对用户更友好,我们可以提供一个明确的名称,而不是使用上下文对象名称.

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

      所以让我们回到我们的 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 并且它仍然有效,但你明白了。

      【讨论】:

        【解决方案4】:

        考虑这两个代码 sn-p

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

        【讨论】:

          猜你喜欢
          • 2011-08-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-21
          • 2010-09-20
          • 1970-01-01
          • 1970-01-01
          • 2011-08-16
          相关资源
          最近更新 更多