2
我已經閱讀了幾乎所有與類似錯誤消息相關的線程,但他們都沒有提供解決方案來解決我的問題。 我是個故事,每個故事都有章節。這個想法是每頁都會有一個章節,但是當我嘗試分頁時,我總是收到相同的錯誤信息「EmptyPage at/story/1/page/1 /」。該頁碼小於1 Django
我對每個故事多個章節,我仍然得到頁碼小於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%}
您的模板是通過頁號作爲查詢參數,但您的網址,查看在URL本身期待它。 –
以及如何解決該問題? –
要一致嗎? –