遍历Django对象和Bootstrap卡

时间:2019-01-03 13:13:19

标签: django twitter-bootstrap bootstrap-4

我想使用Bootstrap cards来按对象创建一张卡,并在每个对象中添加一些sub_object。

例如:

我有一个对象Publication,其中可能包含一个或多个sub_objects Document

发布对象具有一些属性:categorytitlepicturedescription文档对象具有一些属性,例如titleformatlanguage,...

我想得到这样的东西:

enter image description here

对于同一类别,我按出版物创建卡片,并列出每个出版物的所有文档。

这就是我的代码所得到的:

enter image description here

如您所见,我应该在同一张卡中而不是两张不同的卡中包含证件编号1 证件编号2

这是我的代码:

{% for category in research_categories|dictsort:'name' %}
  <div class="row">
    <fieldset>
      <legend id="category_{{ category.id }}"><span class="name">{{ category }}</span></legend>
    </fieldset>
  </div>

    <div class="row">
      <div class="col-sm-4">
        {% for element in test_research %}
          {% if element.publication.category|stringformat:"s"  == category|stringformat:"s" %}
            {% ifchanged %}
              <div class="card" style="width:250px">
                <img class="card-img-top" src="{{ element.publication.cover.url }}" alt="Card image">
                <div class="card-body">
                  <h4 class="card-title">{{ element.publication }}</h4>

                  <table class="table table-condensed">
                    <tbody>
                    <tr>
                      <td> {{ element.title }}</td>
                    </tr>
                    </tbody>
                  </table>
                </div>
              </div>
            {% endifchanged %}
          {% endif %}
        {% endfor %}
      </div>
    </div>
{% endfor %}

根据这部分,我的观点是:

# By default, display documents
test_research = Document.objects.all().order_by('publication__title', 'title', 'language', 'format')

research_categories = defaultdict(list)
for element in test_research:
    research_categories[element.publication.category].append(element)

research_publications = defaultdict(list)
for element in test_research:
    research_publications[element.publication].append(element)

kwargs['test_research'] = test_research
kwargs['research_categories'] = research_categories
kwargs['research_publications'] = research_publications

0 个答案:

没有答案
相关问题