该页码小于1 Django

时间:2017-12-27 18:26:41

标签: python django python-3.x pagination

我已经阅读了几乎所有与类似错误消息相关的线程,但没有一个提供我的问题的解决方案。 我是一个故事,每个故事都有章节。我的想法是每页都会有一个章节,但是当我尝试分页时,我会不断收到相同的错误消息“EmptyPage at / story / 1 / page / 1 /”。

每个故事都有多个章节,我的页码仍然不到1个。

views.py

def post(request, id_story, page=1):
    story = Story.objects.get(id=id_story)
    chapters = story.chapter_set.all()
    paginator = Paginator(chapters, 1)
    try:
        chapters = paginator.page(page)
    except PageNotAnInteger:
        chapters = paginator.page(1)
    except EmptyPage:
        chapters = paginator.page(paginator.num_pages)
    return render(request, 'single.html', {'story': story, 'chapters': chapters})

models.py

class Story(models.Model):
    Lang =(
        ('LA', '-LANGUAGE-'),
        ('Ar', 'ARABIC'),
        ('Ot', 'OTHER')
    )
    title = models.CharField(max_length=250, null=False)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    summary = models.TextField(max_length=1000, null= False)
    pub_date = models.DateField(auto_now_add=True, blank=False, null=False)
    update_time = models.DateField(null=True)
    has_chapter = models.BooleanField(default=False, editable=False)
    lang = models.CharField(choices=Lang, default=Lang[0], max_length=3)
    story_cover = models.FileField()

    def __str__(self):
        return self.title + " - " + self.author.username


class Chapter(models.Model):
    story = models.ForeignKey(Story, on_delete=models.CASCADE)
    chapter_number = models.IntegerField(editable=False, default=1)
    title = models.CharField(max_length=250, null=True)
    chapter = models.TextField()

    def save(self, *args, **kwargs):
        number = Chapter.objects.filter(story=self.story).count()
        self.chapter_number = number + 1
        story = self.story
        if not story.has_chapter:
            story.has_chapter = True
            story.save()
        super(Chapter, self).save(*args,**kwargs)

urls.py

url(r'^story/(?P<id_story>[0-9]+)/page/(?P<page>[0-9]+)/$', views.post, name="post"),

最后是模板

<span class="image featured"><img src="{% static 'images/yogo.jpg'%}" alt=""/></span>
         {% for chapter in chapters %}
                 {% autoescape off%}
                                <h6>{{chapter.title}}</h6>
                               {{chapter.chapter}}
                                {% endautoescape %}
                    {% endfor %}
        <footer>
            <ul class="stats">
                <li><a href="#" class="icon fa-heart">28</a></li>
                <li><a href="#" class="icon fa-comment">128</a></li>
            </ul>
        </footer>
         {% if chapters.has_other_pages %}
        <ul class="actions pagination">
            {% if chapters.has_previous %}
            <li><a href="?page={{ chapters.previous_page_number }}" class=" button big previous">Previous Page</a></li>
            {% else %}
            <li><a href="?page={{ chapters.previous_page_number }}" class="disabled button big previous">Previous Page</a></li>
            {%endif%}
            {% if chapters.has_next %}
            <li><a href="?page={{ chapters.next_page_number }}" class="button big next">Next Page</a></li>
            {%else%}
            <li><a href="?page={{ chapters.next_page_number }}" class="button big next">Next Page</a></li>
            {%endif%}

        </ul>
         {%endif%}

1 个答案:

答案 0 :(得分:1)

您遇到的问题在于您在分页中所做的{%else%}

       {% if chapters.has_previous %}
            <li><a href="?page={{ chapters.previous_page_number }}" class=" button big previous">Previous Page</a></li>
      {% else %}
            <li><a href="?page={{ chapters.previous_page_number }}" class="disabled button big previous">Previous Page</a></li>
      {%endif%}
      {% if chapters.has_next %}
            <li><a href="?page={{ chapters.next_page_number }}" class="button big next">Next Page</a></li>
      {%else%}
            <li><a href="?page={{ chapters.next_page_number }}" class="button big next">Next Page</a></li>
      {%endif%}

当django尝试渲染您的网页并且page = 1到达第一个else语句时,它会发现该网页将是0,这是不正确的,同样的事情将在下一个指令的else处没有下一页的时候。 作为解决方案,它应该是这样的:

 {% if chapters.has_previous %}
            <li><a href="?page={{ chapters.previous_page_number }}" class=" button big previous">Previous Page</a></li>
      {% else %}
            <li><a href="#" class="disabled button big previous">Previous Page</a></li>
      {%endif%}
      {% if chapters.has_next %}
            <li><a href="?page={{ chapters.next_page_number }}" class="button big next">Next Page</a></li>
      {%else%}
            <li><a href="#" class="button big next">Next Page</a></li>
      {%endif%}