python - Pagination on DjangO - No Values
问题描述
I am following a specific guide and basically hit a roadblock I cannot debug.
I implemented Paginator into my blog site and was hoping to show the current page / end of page. But for some reason the page values are blank. What would you think is the reason behind this issue? Do you think that this has something to do with my VS compiler not importing paginator properly?
pagination block code
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}">Previous</a>
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}">Next</a>
{% endif %}
</span>
</div>
views.py
from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .models import Post
# Create your views here.
def post_list(request):
object_list = Post.published.all()
paginator = Paginator(object_list, 3) # 3 posts in each page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
posts = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
return render(request,
'blog/post/list.html',
{'page': page,
'posts': posts})
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request,
'blog/post/detail.html',
{'post': post})
List HTML Code
{% extends "blog/base.html" %}
{% block title %} My Blog {% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% include "pagination.html" with page=post %}
{% endblock %}
解决方案
Try using page=posts
rather than page=post
.
{% include "pagination.html" with page=posts %}
推荐阅读
- java - 双冒号运算符不适用于 Java
- swift - Swift:文本()的.font的条件修饰符
- php - 在 php 中使用数组收集所需部分中的值
- vuejs2 - 试图在 ChartJS 中获取点击栏的 _index
- python - 将可变长度张量修剪到最大长度
- java - 如何在字符串变量中获取特定的 Json 值 - Java?
- sql - SQL sum 将列显示为行
- ruby-on-rails - 如何列出所有父对象的所有子对象?
- python - 在 colab 中转发 twint 命令
- php - 我的 Laravel 5.5 应用程序在部署后无法在远程服务器 namecheap VPS 上运行,但在使用 WAMP 的本地服务器上运行良好