【问题标题】:Exclude a type of post from pagination从分页中排除一种类型的帖子
【发布时间】:2019-04-14 16:00:20
【问题描述】:

我为我的博客帖子使用了一个模型,你可以看到 here(Is a my old post)

如您所见,在该模型中,我可以选择指示突出显示的帖子。如果我使用下面的代码在我的博客上实现分页,那么突出显示的帖子也会在与第一个不同的页面中发送。

  {% for post in posts %}
    {% if post.highlighted == 1 %}
      <h1><strong>Highlighted</strong></h1>
      <a href="{{ post.get_absolute_url }}"><img src="{{ post.header_image }}" alt="Image of {{ post.title }}"></a>
      <h1><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h1>
      <h4>{{ post.tagline }}</h4>
    {% endif %}
  {% endfor %}
<hr><hr><hr>
  {% for post in posts %}
    {% if post.highlighted == 0 %}
      <h1><strong>Not Highlighted</strong></h1>
      <a href="{{ post.get_absolute_url }}"><img src="{{ post.header_image }}" alt="Image of {{ post.title }}"></a>
      <h1><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h1>
      <p>{{ post.tagline }}</p>
      <h5>{{ post.publishing_date|date }}</h5>
      {% for keyconcepts in post.keyconcepts.all %}
        <a href="#">#{{ keyconcepts }}</a>&nbsp;
      {% endfor %}
      <hr>
    {% endif %}

  {% endfor %}

  {% block pagination %}
    {% if is_paginated %}
      <div class="pagination px-auto">
        <nav aria-label="Page navigation">
          <ul class="pagination justify-content-center">
              {% if page_obj.has_previous %}
              <li class="page-item">
                <a class="page-link text-center shadow" href="{{ request.path }}?page={{ page_obj.previous_page_number }}">Pagina precedente</a>
              </li>
              {% endif %}
              <li class="page-item disabled">
                <p class="page-link text-center shadow">Pagina {{ page_obj.number }} di {{ page_obj.paginator.num_pages }}.</p>
              </li>
              {% if page_obj.has_next %}
              <li class="page-item">
                <a class="page-link text-center shadow" href="{{ request.path }}?page={{ page_obj.next_page_number }}">Pagina successiva</a>
              </li>
              {% endif %}
          </ul>
        </nav>
      </div>
    {% endif %}
  {% endblock %}

我想从分页中排除所有突出显示的帖子。有可能吗?

views.py

下面
class ListPost(ListView):
    model = Blog
    context_object_name = 'posts'
    queryset = Blog.objects.filter(category="G.I.S.") #FUNDAMENTAL FILTER
    template_name = "blog/list_post.html"
    paginate_by = 3

【问题讨论】:

  • 但是 Willem 在你原来的帖子中给了你这个答案,除了你需要选择 is_highlighted=False。
  • 相对于我的旧帖子,我为 html 模板采用了不同的解决方案。我已经更新了html代码,抱歉我的错误。

标签: django pagination django-templates django-views django-2.1


【解决方案1】:
class ListPost(ListView):
    queryset = Blog.objects.filter(is_highlighted=True)
    context_object_name = 'posts'
    template_name = "blog/list_post.html"
    paginate_by = 3

注意Blog 似乎用词不当。 BlogPost 似乎更准确。

【讨论】:

  • 您的建议不正确。我不只想要突出显示的帖子,而是“GIS”类别的所有帖子以及带有突出显示帖子的分页,始终位于第一页帖子列表的顶部。
  • 你没有提到任何关于category的事情,直到我回答问题后你edited it:|
  • 为什么不做queryset = Blog.objects.filter(category="G.I.S.", is_highlighted=True)
  • 因为我有这个:django.core.exceptions.FieldError: Cannot resolve keyword 'is_highlighted' into field。选项有:类别、内容、草稿、header_image、突出显示、id、keyconcepts、post_ptr、post_ptr_id、publishing_date、sl ug、标语、timeaction_ptr、timeaction_ptr_id、title、updated_date
  • 答案在错误中 - 您的字段名称是 highlighted,而不是 is_highlighted ? 所以它是 queryset = Blog.objects.filter(category="G.I.S.", highlighted=True)
猜你喜欢
  • 2021-08-07
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多