该页码小于1 Django

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

回答:

问题你在两个{%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,这是不正确的,同样的事情将是,当没有它下一页指令的其他部分。 作为一种解决方案应该是这样的:

{% 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 的全部内容, 来源链接: utcz.com/qa/266881.html

回到顶部