0

我为我的博客帖子使用了一个模型,您可以在此处看到(是我的旧帖子)

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

  {% 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
4

1 回答 1

0
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似乎更准确。

于 2018-11-12T00:58:49.633 回答