该页码小于1 Django
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了该页码小于1 Django相关的知识,希望对你有一定的参考价值。
我已经阅读了几乎所有与类似错误消息相关的线程,但没有一个提供我的问题的解决方案。我是一个故事,每个故事都有章节。我的想法是每页都会有一个章节,但是当我尝试分页时,我会不断收到相同的错误消息“EmptyPage at / story / 1 / page / 1 /”。
每个故事我有多个章节,我仍然得到页码小于1。
views.朋友
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.朋友
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.朋友
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%}
s。
{% 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,这是不正确的,同样的事情是当它没有下一页在其他地方为下一条指令。作为解决方案,它应该是这样的:
{% 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%}
以上是关于该页码小于1 Django的主要内容,如果未能解决你的问题,请参考以下文章
从 html 转换的 pdf 中的页码 - pdfkit、python/django