python - 我的 django 分页器不返回任何输出
问题描述
我有两个分页器:第一个在我的文章页面中,第二个在根据文章类别对文章进行分类的页面中。第一个运行良好,但是当我查看指定类别的文章时,我发现我的分页器无法正常工作。我提前感谢;#views.py
from django.core.paginator import Paginator
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, JsonResponse, Http404
from .models import Article, Category
# Create your views here.
def home(request):
context = {
"articles": Article.objects.published()
}
return render(request, 'website/home.html', context)
def detail(request, slug):
context = {
"article": get_object_or_404(Article.objects.published(), slug=slug)
}
return render(request, 'website/detail.html', context)
def article(request, page=1):
articles_list = Article.objects.published()
paginator = Paginator(articles_list, 1)
articles = paginator.get_page(page)
context = {
"articles": articles,
"category": Category.objects.filter(status=True)
}
return render(request, 'website/article.html', context)
def category(request, slug, page_cat=1):
cat = get_object_or_404(Category, slug=slug, status=True)
articles_cat = cat.articles.filter(status='Published')
paginator_cat = Paginator(articles_cat, 1)
cat_articles = paginator_cat.get_page(page_cat)
context = {
"category": cat_articles
}
return render(request, 'website/category.html', context)
######我的category.html文件
<div class="blog-pagination">
<ul class="justify-content-center">
{% if cat_articles.has_previous %}
<li><a
href="{% url 'website:category' cat_articles.previous_page_number %}">
<i class="icofont-rounded-left"
></i></a></li>
{% endif %}
<li><a href="#">1</a></li>
<li class="#"><a href="#">2</a></li>
<li><a href="#">3</a></li>
{% if cat_articles.has_next %}
<li><a href="{% url 'website:category' cat_articles.next_page_number %}">
<i class="icofont-rounded-right"
></i></a></li>
{% endif %}
</ul>
</div>
############### 我的urls.py
from django.urls import path
from .views import home, detail, article, category
app_name = 'website'
urlpatterns = [
path('', home, name='home'),
path('article/<int:page>', article, name='article'),
path('article/<slug:slug>', detail, name='detail'),
path('article', article, name='article'),
path('category/<int:page_cat>', category, name='category'),
path('category/<slug:slug>', category, name='category')
]
解决方案
推荐阅读
- mapbox - Mapbox 以编程方式添加不同高度的多边形
- flutter - 如何通过变量引用实例化一个类?
- python - 尝试安装我的 android 应用程序时收到错误消息
- substrate - 您如何接受带有墨水的文件?
- javascript - 使用 setInterval 时无法将事件添加到 DataTable 分页按钮
- r - 将复杂数据重塑为宽格式,其中输入是长数据和宽数据的混合
- python - 尝试导入繁重的 python 文件时系统停止响应
- javascript - Tone.js 应用程序 - 直到第二次点击音频才会开始
- javascript - Chart.js、Chrome、Javalin:语法错误 - 输入意外结束
- javascript - 'npm 是否安装'l
安装最新版本的包还是与安装的节点版本兼容的最新版本?