为什么我的页面即使在应用分页后也显示所有博客条目

问题描述 投票:0回答:2

请在这里帮助。

我正在使用pycharm和django编程。

我正在我的应用程序的页面中显示博客条目。

即使将视图设置为每页显示2个条目,所有条目仍保持显示。

您认为错误来自何处?

我不确定是否与模板渲染有关,或者可能与

分页器逻辑?

Views.py-这是博客显示的完整视图。

def blog(request):
profile = About_Us_Model.objects.all()
our_services = Services_Model.objects.all()
blog_post = Blog_Post_Model.objects.filter(status=1)
blog_post_cat = Blog_Category_Model.objects.all()
tags = Blog_Post_Model.tags.all
newsletter_form = NewsLetterForm()
new_member = None

page = request.GET.get('page', 1)
paginator = Paginator(blog_post,2)

try:
    blog_page = paginator.page(page)
except PageNotAnInteger:
    blog_page = paginator.page(1)
except EmptyPage:
    blog_page = paginator.page(paginator.num_pages)

if request.method == 'POST':
    newsletter_form = NewsLetterForm(data=request.POST)
    if newsletter_form.is_valid():
        new_member = newsletter_form.save()
        new_member.save()
    else:
        newsletter_form = NewsLetterForm()

context = {
    'profile': profile,
    'blog_post': blog_post,
    'services': our_services,
    'categories': blog_post_cat,
    'tags': tags,
    'newsletter': newsletter_form,
    'new_member': new_member,
    'blog':blog_page,
    'page':page,
}
return render(request, 'webpages/blog.html', context=context)

Blog.html(博客条目)-在这里显示了博客条目

<div class="col-lg-8 entries">
            {% for post in blog_post %}
            <article class="entry" data-aos="fade-up">
              <div class="entry-img">
                <img src="{{post.image.url }}" alt="" class="img-fluid">
              </div>

              <h2 class="entry-title">
                <a href="{% url 'webpages:articles_detail' post.pk %}">{{post.title}}</a>
              </h2>

              <div class="entry-meta">
                <ul>
                  <li class="d-flex align-items-center"><i class="icofont-user"></i> <a href="{% url 'webpages:articles_detail' post.pk %}">{{post.author}}</a></li>
                  <li class="d-flex align-items-center"><i class="icofont-wall-clock"></i> <a href="{% url 'webpages:articles_detail' post.pk %}"><time datetime="2020-01-01">{{post.created_on | date:"Y-m-d"}}</time></a></li>
                  <li class="d-flex align-items-center"><i class="icofont-comment"></i> <a href="{% url 'webpages:articles_detail' post.pk %}">12 Comments</a></li>
                </ul>
              </div>

              <div class="entry-content">
                <p>
                  {{post.body | safe | linebreaks | truncatewords:20 }}
                </p>
                <div class="read-more">
                  <a href="{% url 'webpages:articles_detail' post.pk %}">Read More</a>
                </div>
              </div>
            </article><!-- End blog entry -->
          {% endfor %}

Blog.html(分页)

<div class="blog-pagination">
    {% if blog.has_other_pages %}

       <ul class="justify-content-center">
         {% if blog.has_previous %}
            <li><a href="?page={{ blog.previous_page_number }}"><i class="icofont-rounded-left"></i></a></li>
         {% else %}

         <li class="disabled"><i class="icofont-rounded-left"></i></li>
     {% endif %}

     {% for i in blog.paginator.page_range %}
       {% if blog.number == i %}
         <li class="active"><a href="">{{ i }}</a></li>
       {% else %}
       <li><a href="?page={{ i }}">{{ i }}</a></li>
      {% endif %}
     {% endfor %}

      {% if blog.has_next %}
       <li><a href="?page={{ blog.next_page_number }}"><i class="icofont-rounded-right"></i></a></li></a></li>
      {% else %}
      <li class="disabled"><i class="icofont-rounded-right"></i></a></li></li>
      {% endif %}
   </ul>
 {% endif %}

django pagination blogs
2个回答
0
投票

blog_post仍然是包含所有Blog_Post_Model对象的查询集,您需要遍历Page对象而不是查询集,例如:

{% for post in blog %}
    <!-- … -->
{% endfor %}

而不是:

{% for post in blog_post %}
    <!-- … -->
{% endfor %}

QuerySet的(或多或少)immutable,所以分页器不会alter查询集,它会在Page对象中创建一个分页的查询集。


0
投票

尝试一下:

def blog(request):
profile = About_Us_Model.objects.all()
our_services = Services_Model.objects.all()

blog_post_cat = Blog_Category_Model.objects.all()
tags = Blog_Post_Model.tags.all
newsletter_form = NewsLetterForm()
new_member = None
blog_post = Blog_Post_Model.objects.filter(status=1)
paginator = Paginator(blog_post,2)
page = request.GET.get('page', 1)


try:
    blog_page = paginator.get_page(page)
except PageNotAnInteger:
    blog_page = paginator.get_page(1)
except EmptyPage:
    blog_page = paginator.get_page(paginator.num_pages)

if request.method == 'POST':
    newsletter_form = NewsLetterForm(data=request.POST)
    if newsletter_form.is_valid():
        new_member = newsletter_form.save()
        new_member.save()
    else:
        newsletter_form = NewsLetterForm()

context = {
    'profile': profile,
    'blog_post': blog_post,
    'services': our_services,
    'categories': blog_post_cat,
    'tags': tags,
    'newsletter': newsletter_form,
    'new_member': new_member,
    'blog':blog_page,
    'page':page,
}
return render(request, 'webpages/blog.html', context=context)
© www.soinside.com 2019 - 2024. All rights reserved.