gpt4 book ai didi

python - 该页码小于 1 Django

转载 作者:太空宇宙 更新时间:2023-11-04 04:52:06 27 4
gpt4 key购买 nike

我已经阅读了几乎所有与类似错误消息相关的线程,但没有一个提供了解决我的问题的方法。我是一个故事,每个故事都有章节。我的想法是每页会有一个章节,但当我尝试分页时,我不断收到相同的错误消息“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})

模型.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%}

最佳答案

您面临的问题在于您在分页中执行的两个 {%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%}

关于python - 该页码小于 1 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47996963/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com