【问题标题】:is_paginated not working properly for class based views in Djangois_paginated 对于 Django 中基于类的视图不能正常工作
【发布时间】:2017-11-10 23:30:49
【问题描述】:

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 %}

views.py 中的类:

class BookListView(generic.ListView):
  model = Book
  paginate_by = 2
  queryset =Book.objects.all()

urls.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'),

]

图书模型:

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)] )  

book_list 完美呈现,问题出在分页器上,is_paginated 条件不起作用,它执行 else 语句,我已经尝试了 3 个多小时,但找不到解决方案,我在做什么这里不见了?
Django 版本:1.11.2 蟒蛇:3.5

编辑: 更新 1:问题是 paginate_by 值是两个,并且要显示的总项目也是两个因此它没有启动 is_paginated 标签,当我添加一个项目超过 paginate_by 值时它工作正常。

【问题讨论】:

    标签: django django-views django-generic-views


    【解决方案1】:

    使用这个,你的 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 %}
    

    【讨论】:

    • 将您的查询集放在 paginate_by 上方的视图中并检查
    • book_list 是否应该像查询集一样被查询集?您是否更改了上下文名称?在通过之前的视图中?
    • 不,我没有更改上下文名称。
    • 所以代替 if book_list 使用 if queryset,更新了 ans @KishyNivas
    • 这一次,执行了最后一个 else 语句,即 'No book '。
    猜你喜欢
    • 2019-02-07
    • 2016-01-30
    • 2018-05-20
    • 2021-08-03
    • 2017-05-19
    • 2020-12-25
    • 2015-08-25
    • 2020-10-11
    • 2018-05-14
    相关资源
    最近更新 更多