Django和HTML模板:按面板划分具有共同对象属性的面板

时间:2018-10-23 13:44:27

标签: django

再过一次,我再次与您联系,以获取建议或帮助。 我正在django模板中显示一个对象列表,我想通过一个公共属性category对它们进行排序。

显示的每个对象(出版物)都具有一些属性:categoryformatlanguage ...

例如:

enter image description here

带有蓝色背景的白色文本表示类别。我有2个出版物的category = BIOLOGICAL STANDARDISATION PROGRAMME和1个出版物的category = TEST

我想将BIOLOGICAL STANDARDISATION PROGRAMME都分组在一个面板中,但是我找不到解决方法。

这是我的HTML模板文件:

{% for element in test_research|dictsort:"publication.category.name" %}
          <div class="col-sm-12">
            <div class="panel panel-default request-panel">
              <div class="panel-heading" role="tab">
                <h4 class="panel-title">
                  {{ element.publication.category }}
                </h4>
              </div>

              <div class="panel-body">
                <div class="row">
                  <div class="col-sm-9">
                    <p class="request-publication">{{ element.publication }} </p>
                  </div>
                  <div class="col-sm-3 request-cover">
                    {% if element.publication.cover %}
                      <a href="{{ element.publication.cover.url }}" target="_blank">
                        {% thumbnail element.publication.cover "40x40" crop="center" as im %}
                          <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
                        {% endthumbnail %}</a>
                    {% endif %}

                  </div>
                </div>
              </div>
              <div class="panel-footer">
                <div class="row">
                  <table>
                    <tbody>
                    <tr>
                      <td class="col-md-1">
                        <div class="material-switch pull-right">
                          <input id="someSwitchOptionSuccess_{{ element.id }}" name="DocumentChoice" type="checkbox"
                                 value="{{ element.id }}"/>
                          <label for="someSwitchOptionSuccess_{{ element.id }}" class="label-success"></label>
                        </div>
                      </td>
                      <td class="col-md-1 request-language"> {{ element.language }}</td>
                      <td class="col-md-1 request-format">
                        {% if element.format == 'pdf' %}
                          <span class="badge alert-danger">{{ element.format }}</span>
                        {% endif %}
                        {% if element.format == 'epub' %}
                          <span class="badge alert-info">{{ element.format }}</span>
                        {% endif %}
                      </td>
                      <td class="col-md-1 request-flag">
                        {% if element.publication.new_publication == True %}
                          <span class="glyphicon glyphicon-flag"></span>
                        {% else %}
                          <span></span>
                        {% endif %}
                      </td>
                      <td class="col-md-offset-5 col-md-3 text-right">{{ element.title }}</td>
                    </tr>
                    </tbody>
                  </table>

                </div>
              </div>
            </div>
          </div>
        {% endfor %}

在我的views.py文件中:

def get_context_data(self, **kwargs):

    search_category = Document.objects.values_list('publication__category__name', flat=True).distinct()
    kwargs['search_category'] = search_category

    search_format = Document.objects.values_list('format', flat=True).distinct()
    kwargs['search_format'] = search_format

    search_language = Document.objects.values_list('language', flat=True).distinct()
    kwargs['search_language'] = search_language

    checkbox_category = self.request.GET.getlist('CategoryChoice')
    checkbox_format = self.request.GET.getlist('FormatChoice')
    checkbox_language = self.request.GET.getlist('LanguageChoice')
    choice_title = self.request.GET.get('TitleChoice')

    kwargs['checkbox_category'] = checkbox_category
    kwargs['checkbox_format'] = checkbox_format
    kwargs['checkbox_language'] = checkbox_language
    kwargs['choice_title'] = choice_title

    # default to all documents

    test_research = Document.objects.all().order_by('publication__category__name')
    kwargs['test_research'] = test_research

    if "SubmitChoice" in self.request.GET:

        test_research = Document.objects.all()

        # if user entered any search criteria, add those filters
        if checkbox_category:
            test_research = test_research.filter(publication__category__name__in=checkbox_category)

        if checkbox_format:
            test_research = test_research.filter(format__in=checkbox_format)

        if checkbox_language:
            test_research = test_research.filter(language__in=checkbox_language)

        if choice_title:
            test_research = test_research.filter(
                Q(title__icontains=choice_title) | Q(publication__title__icontains=choice_title))

        kwargs['test_research'] = test_research

    return super(HomeView, self).get_context_data(**kwargs)

如有必要,我可以添加models.py文件。如何将它们分组在同一类别面板下?

编辑:

我也许发现了一些东西:

{% for category in checkbox_category %}
      <div class="col-sm-12">
        <div class="panel panel-default request-panel">
          <div class="panel-heading" role="tab">
            <h4 class="panel-title">
              {{ category }}
            </h4>
          </div>

          {% for element in test_research %}
            {{ element.publication.category }} - {{ category }}
            {% if element.publication.category == category %}

但是即使if的{​​{1}}条件似乎也不起作用

1 个答案:

答案 0 :(得分:1)

您应该在视图中重组数据,以便为模板做好准备。 Django的模板系统的构建可避免这种逻辑。

您也许可以像这样简单地做到这一点:

from collections import defaultdict

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

然后在模板中使用research_categories