【问题标题】:Paginator url erorr in djangodjango中的分页url错误
【发布时间】:2021-02-09 19:24:11
【问题描述】:

我正在尝试为我的文章 django 页面制作分页器。但是当我尝试转到下一页时,我收到一个错误(“找不到页面”和“没有文章与给定的查询匹配。”)而且我不知道我做错了什么。我感谢您的帮助... ########## 我的意见.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, 2)
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):
cat = get_object_or_404(Category, slug=slug, status=True)
context = {
    "category": cat.articles.filter(status='Published')
}
return render(request, 'website/category.html', context)

####### 我的 urls.py

from django.urls import path
from .views import home, detail, article, category

app_name = 'website'
urlpatterns = [
path('', home, name='home'),
path('article/<slug:slug>', detail, name='detail'),
path('article', article, name='article'),
path('article/<int:page>', article, name='article'),
path('category/<slug:slug>', category, name='category')
]

article.html页面相关部分:

  <div class="blog-pagination">
          <ul class="justify-content-center">
            {% if articles.has_previous %}
              <li class="disabled"><a>
                  href="{% url 'website:article' 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 articles.has_next %}
          <li><a href="{% url 'website:article' articles.next_page_number %}">
              <i class="icofont-rounded-right"
                 ></i></a></li>
          {% endif %}
          </ul>
        </div>

【问题讨论】:

    标签: python django django-views django-urls django-pagination


    【解决方案1】:

    Ulrs 一个接一个地被检查,所以

    path('article/<slug:slug>', detail, name='detail'),
    

    已使用,请尝试将其替换为

    path('article/<int:page>', article, name='article'),
    

    我的意思是 int 应该在 urlpaterns 列表中排在第一位

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 2017-07-24
      • 2013-03-12
      • 2015-10-20
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      相关资源
      最近更新 更多