对于Django中基于类的视图,分页是否工作不正常

对于Django中基于类的视图,分页是否工作不正常,django,django-views,django-generic-views,Django,Django Views,Django Generic Views,book_list.html {% extends "base.html" %} {% block content %} <h3> Available books </h3> {% if book_list %} <ul> {% for book in book_list %} <li> <a href = "{{ book.get_absolute_url }}">{{book.title }}</a>

book_list.html

{% extends "base.html" %}
{% block content %}
<h3> Available books </h3>
{% if book_list %}
<ul>
    {% for book in book_list %}
     <li> <a href = "{{ book.get_absolute_url }}">{{book.title }}</a>    <small> by {{book.author }}</small></li>
     <p>{{ book.summary }}
    {% endfor %}
<ul>
{% endif %}
{% if is_paginated %}
  <div class="pagination">
      <span class="page-links">
          {% if page_obj.has_previous %}
              <a href="{{ request.path }}?page={{ page_obj.previous_page_number }}">previous</a>
          {% endif %}
          <span class="page-current">
              Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
          </span>
          {% if page_obj.has_next %}
              <a href="{{ request.path }}?page={{ page_obj.next_page_number }}">next</a>
          {% endif %}
      </span>
  </div>
{% else %}
      <h4> pagination not working</h4>
{% endif %}
{% endblock %}
URL.py:

 urlpatterns =[
 url('^$',views.index, name ='index'),  # matching with an empty string 
 url('^books/$',views.BookListView.as_view(),name ='books'), #the one to which I am adding the paginator
 url('^book/(?P<pk>\d+)/$',views.BookDetailView.as_view(),name='book-detail'),

]
urlpatterns=[
url(“^$”,views.index,name='index'),#与空字符串匹配
url(“^books/$”,views.BookListView.as_view(),name='books'),#我要向其中添加分页器的url
url(“^book/(?P\d+/$”,views.BookDetailView.as_view(),name='book-detail'),
]
图书型号:

class Book(models.Model):
  title=models.CharField(max_length=100)
  author = models.ForeignKey('Author',on_delete=models.SET_NULL,null = True)
  summary = models.TextField(max_length=200,help_text="Enter the description")
  isbn = models.CharField('ISBN',max_length=13 ,help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn">ISBN number</a>' )
  genre = models.ManyToManyField(Genre,help_text = 'selct a genre for this book')

  class Meta:
     ordering =["title"]
  def __str__(self):
     return self.title

  def get_absolute_url(self):
     return reverse('book-detail',args=[str(self.id)] )  
教材(models.Model):
title=models.CharField(最大长度=100)
author=models.ForeignKey('author',on_delete=models.SET_NULL,NULL=True)
summary=models.TextField(最大长度=200,help\u text=“输入描述”)
isbn=models.CharField('isbn',最大长度=13,帮助文本=13个字符')
流派=模型.ManyToManyField(流派,帮助\文本='为本书选择流派')
类元:
排序=[“标题”]
定义(自我):
返回自己的标题
def get_绝对_url(自身):
返回反向('book-detail',args=[str(self.id)])
图书列表呈现得非常完美,问题在于分页器,is paginated条件不起作用,它执行else语句,我已经尝试了3个多小时,但找不到解决方案,我缺少什么?
Django版本:1.11.2 Python:3.5

编辑:
更新1:问题是paginate_by value是两个,要显示的项目总数也是两个,因此它没有启动is_paginate标记,当我比paginate_by value多添加一个项目时,它工作正常。

使用此选项,您在if条件下遇到了一些问题

{% extends "base.html" %}
{% block content %}
<h3> Available books </h3>
{% if object_list %}
    <ul>
        {% for book in object_list %}
         <li> <a href = "{{ book.get_absolute_url }}">{{book.title }}</a>    <small> by {{book.author }}</small></li>
         <p>{{ book.summary }}
        {% endfor %}
    <ul>

    {% if is_paginated %}
      <div class="pagination">
          <span class="page-links">
              {% if page_obj.has_previous %}
                  <a href="{{ request.path }}?page={{ page_obj.previous_page_number }}">previous</a>
              {% endif %}
              <span class="page-current">
                  Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
              </span>
              {% if page_obj.has_next %}
                  <a href="{{ request.path }}?page={{ page_obj.next_page_number }}">next</a>
              {% endif %}
          </span>
      </div>
    {% else %}
          <h4> pagination not working</h4>
    {% endif %}
        {% else %}
        <h4> No book</h4>
{% endif %}
{% endblock %}
{%extends“base.html”%}
{%block content%}
现有书籍
{%if对象\列表%}
    {对象_列表%]中的书本的百分比}
  • 作者{{book.author}
  • {{book.summary}} {%endfor%}
      {%if已分页%} {%如果页面_obj.has_previous%} {%endif%} 第{{Page_obj.paginator.num_pages}页中的第{{Page_obj.number}页。 {%如果页面_obj.has_next%} {%endif%} {%else%} 分页不起作用 {%endif%} {%else%} 没有书 {%endif%} {%endblock%}
将您的查询集放在paginate\u by上方的视图中,并选中图书列表是否应该像queryset一样进行查询设置?您是否更改了上下文名称?在通过之前的视图中?不,我没有更改上下文名称。因此,我这次更新了ans@kishynivast,而不是if book_list use if queryset,最后一个else语句被执行,即“No book”。
{% extends "base.html" %}
{% block content %}
<h3> Available books </h3>
{% if object_list %}
    <ul>
        {% for book in object_list %}
         <li> <a href = "{{ book.get_absolute_url }}">{{book.title }}</a>    <small> by {{book.author }}</small></li>
         <p>{{ book.summary }}
        {% endfor %}
    <ul>

    {% if is_paginated %}
      <div class="pagination">
          <span class="page-links">
              {% if page_obj.has_previous %}
                  <a href="{{ request.path }}?page={{ page_obj.previous_page_number }}">previous</a>
              {% endif %}
              <span class="page-current">
                  Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
              </span>
              {% if page_obj.has_next %}
                  <a href="{{ request.path }}?page={{ page_obj.next_page_number }}">next</a>
              {% endif %}
          </span>
      </div>
    {% else %}
          <h4> pagination not working</h4>
    {% endif %}
        {% else %}
        <h4> No book</h4>
{% endif %}
{% endblock %}