【问题标题】:Django Class Based Listview with two filtered individual lists具有两个过滤的单独列表的基于 Django 类的列表视图
【发布时间】:2018-06-01 05:12:34
【问题描述】:

您好,我正在尝试创建一个使用模型 Item 的页面,该模型具有一个名为 data_type 的模型字段,其中整数选择选项为 1 或 2。我目前有一个 Listview 页面,我正在尝试创建两个列表,一个显示 data_type=1 的项目,另一个显示 data_type=2。

1 代表模型,2 代表脚本,是否有帮助?

这是我的意见.py

class ItemListView(generic.ListView):


    context_object_name = 'model_list'
    queryset = Item.objects.filter(data_type='1')

    context_object_name = 'script_list'
    queryset = Item.objects.filter(data_type='2')

    template_name = 'item_list.html',

Item_list.html

<h3 class="text-muted text-center mt-4">Models</h3>
<hr>
<div class="container">
  <div class="row mt-4">
  {% if model_list %}
    {% for item in model_list %}
  <div class="col-md-3">
    <div class="card card-primary card-effects p-2 mb-3 mt-4">
      <a href="{% url 'portal:item_detail' item.pk %}" class="card-full-link">
      <img class="card-img" src="{{ item.image.url }}" alt="Generic placeholder image" width="225" height="185">
      <div class="card-body">
        <p class="text-muted"> {{ item.get_data_type_display }}</p>
        <h4 class="text-left"> {{ item.name }}<span class="text-blue">Ξ{{ item.price }}</span></h4>
      </div>
      </a>
      <!-- <p><a class="btn btn-success" href="#" role="button">Learn more &raquo;</a></p> !-->
    </div>
  </div>
          {% endfor %}
    {% else %}
      <p>There are no items in the database.</p>
    {% endif %}


  </div>
</div>

<h3 class="text-muted text-center mt-4">Scripts </h3>
<hr>
<div class="container">
  <div class="row mt-4">
  {% if script_list %}
    {% for item in script_list %}
  <div class="col-md-3">
    <div class="card card-primary card-effects p-2 mb-3 mt-4">
      <a href="{% url 'portal:item_detail' item.pk %}" class="card-full-link">
      <img class="card-img" src="{{ item.image.url }}" alt="Generic placeholder image" width="225" height="185">
      <div class="card-body">
        <p class="text-muted"> {{ item.get_data_type_display }}</p>
        <h4 class="text-center"> {{ item.name }}<span class="text-blue">Ξ{{ item.price }}</span></h4>
      </div>
      </a>
      <!-- <p><a class="btn btn-success" href="#" role="button">Learn more &raquo;</a></p> !-->
    </div>
  </div>
          {% endfor %}
    {% else %}
      <p>There are no items in the database.</p>
    {% endif %}


  </div>
</div>

希望 views.py 能让您了解我想要做什么。看来我不能有两个 context_object_name 和查询集。如果我删除 vottom context_object_name 和 queryset,model_list 将加载。

我从 django docs https://docs.djangoproject.com/en/2.0/topics/class-based-views/generic-display/ 得到了 context_object_name 查询集的想法

【问题讨论】:

    标签: django class django-queryset


    【解决方案1】:

    你可以覆盖get_context_data方法:

    class ItemListView(generic.ListView):
    
        context_object_name = 'model_list'
        queryset = Item.objects.filter(data_type='1')
    
        template_name = 'item_list.html'
    
        def get_context_data(self, **kwargs):
            context = super(ItemListView, self).get_context_data(**kwargs)
            context['script_list'] = Item.objects.filter(data_type='2')
            return context  
    

    【讨论】:

    • 啊,谢谢。这是有道理的,不知道为什么我没有想到使用 get_context_data!
    猜你喜欢
    • 1970-01-01
    • 2013-08-09
    • 2014-01-08
    • 2014-02-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2011-05-28
    相关资源
    最近更新 更多