【问题标题】:Problem pagination for category posts in DjangoDjango中类别帖子的问题分页
【发布时间】:2020-07-28 22:02:15
【问题描述】:

我使用以下代码进行分页并得到了我留下的错误。请帮忙。 注意,对于帖子数小于2(_paginator)的分类,是没有问题的,但是同样的帖子超过2个,分页就完成了。

urls.py:

from django.urls import path
from . views import home, detail_article, category_list

app_name = "blog"
urlpatterns = [
       path('', home, name="home"),
       path('page/<int:page>', home, name="home"),
       path('post/<slug:slug>', detail_article, name="detail_article"),
       path('category/<slug:slug>', category_list, name="category_list"),
       path('category/<slug:slug>/page/<int:page>', category_list, name="category_list")
]

views.py:

def category_list(request, slug, page=1):
category = get_object_or_404(Category, slug=slug, status=True)
article_list = category.articles.published()
_paginator = Paginator(article_list, 2)
articles = _paginator.get_page(page)
context = {
    'category':category,
    'articles':articles
}
return render(request, 'blog/category.html', context)

模板.html:

<!-- Paginator -->
                    {% if articles.has_other_pages %}
                    <div class="col-12 pagging">
                        <ul>
                            {% if articles.has_previous %}
                            <li>
                                <a href="{% url 'blog:category_list' category.slug articles.previous_page_number %}">
                                    <i class="fa fa-arrow-right" aria-hidden="true"></i></a>
                            </li>
                            {% else %}
                            <li><i class="fa fa-arrow-right a-active" aria-hidden="true"></i></li>
                            {% endif %} 
                            <!-- {% for i in articles.paginator.page_range %} {% if articles.number == i %}
                            <li><span class="a-active">{{ i }}</span></li>
                            {% else %}
                            <li><a href="{% url 'blog:category'  %} page/{{ i }}">{{ i }}</a></li>
                            {% endif %} {% endfor %}  -->
                            {% if articles.has_next %}
                            <li>
                                <a href="{% url 'blog:category_list' category.slug articles.next_page_number %}">
                                    <i class="fa fa-arrow-left" aria-hidden="true"></i>
                                </a>
                            </li>
                            {% else %}
                            <li><i class="fa fa-arrow-left a-active" aria-hidden="true"></i></li>
                            {% endif %}
                        </ul>
                    </div>
                    {% endif %}
                    <!-- end paginator -->

和错误! :( error

【问题讨论】:

  • comment 中的 HTML 仍然由模板引擎渲染。

标签: django pagination


【解决方案1】:

comment 中的 HTML 仍然由模板引擎呈现。所以你应该删除{% url 'blog:category' %} 部分。如果您想在 Django 模板中添加注释,请在 {# … #} 之间进行,或者您可以使用 {% comment %} … {% endcomment %} template block [Django-doc]

例如:

{% if articles.has_previous %}
<li>
    <a href="{% url 'blog:category_list' category.slug articles.previous_page_number %}">
        <i class="fa fa-arrow-right" aria-hidden="true"></i></a>
</li>
{% else %}
<li><i class="fa fa-arrow-right a-active" aria-hidden="true"></i></li>
{% endif %}

{% comment %}
<!-- {% for i in articles.paginator.page_range %} {% if articles.number == i %}
<li><span class="a-active">{{ i }}</span></li>
{% else %}
<li><a href="{% url 'blog:category'  %} page/{{ i }}">{{ i }}</a></li>
{% endif %} {% endfor %}  -->
{% endcomment %}

{% if articles.has_next %}
<li>
    <a href="{% url 'blog:category_list' category.slug articles.next_page_number %}">
        <i class="fa fa-arrow-left" aria-hidden="true"></i>
    </a>
</li>
{% else %}
<li><i class="fa fa-arrow-left a-active" aria-hidden="true"></i></li>
{% endif %}

【讨论】:

  • 坦克。我删除了 {% url 'blog:category' %} 部分,但仍然存在这个问题!
  • @Remo:很可能是另一个问题:) 或者至少在另一个位置(不包括在内)。
【解决方案2】:

已修复问题! :)

                               {% for i in articles.paginator.page_range %} 
                           {% if articles.number == i %}
                            <li><span class="a-active">{{ i }}</span></li>
                            {% else %}
                            <li><a href="{% url 'blog:home'   %}category/{{category.slug}}/page/{{ i }}">{{ i }}</a></li>
                            {% endif %} {% endfor %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-04
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多