Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 视图函数Django中的分页器_Python_Django_Web_Pagination - Fatal编程技术网

Python 视图函数Django中的分页器

Python 视图函数Django中的分页器,python,django,web,pagination,Python,Django,Web,Pagination,我正在尝试从我的博客网站在我的帖子页面上添加paginate选项。我想在我的视图函数中实现paginator来呈现页面,我从Django的文档中找到了这个示例,但它不起作用。有什么帮助吗 查看功能: def blog(request): posts = Post.objects.all().order_by('-date_posted') paginator = Paginator(posts, 2) page_number = request.GET.get('page

我正在尝试从我的博客网站在我的帖子页面上添加paginate选项。我想在我的视图函数中实现paginator来呈现页面,我从Django的文档中找到了这个示例,但它不起作用。有什么帮助吗

查看功能:

def blog(request):
    posts = Post.objects.all().order_by('-date_posted')
    paginator = Paginator(posts, 2)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    context = {
        'posts': posts,
        'page_obj': page_obj,
        'title': 'Blog',
        'banner_page_title': 'Blog',
        'page_location': 'Home / Blog'
    }
    return render(request, 'blog/blog.html', context)
html呈现

<nav class="blog-pagination justify-content-center d-flex">
  <ul class="pagination">
      {% if is_paginated %}
        {% if page_obj.has_previous %}
          <li class="page-item">
              <a href="?page={{ page_obj.previous_page_number }}" class="page-link" aria-label="Previous">
                  <span aria-hidden="true">
                      <span class="ti-arrow-left"></span>
                  </span>
              </a>
          </li>
        {% endif %}

      {% for num in page_obj.paginator.page_range %}
        {% if page_obj.number == num %}
          <li class="page-item active">
              <a href="?page={{ num }}" class="page-link">{{ num }}</a>
          </li>
        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
          <li class="page-item">
            <a href="?page={{ num }}" class="page-link">{{ num }}</a>
        </li>
        {% endif %}
      {% endfor %}

      {% if page_obj.has_next %}
        <li class="page-item">
            <a href="?page={{ page_obj.next_page_number }}" class="page-link" aria-label="Next">
                <span aria-hidden="true">
                    <span class="ti-arrow-right"></span>
                </span>
            </a>
        </li>
      {% endif %}
    {% endif %}
  </ul>
</nav>

删除
{%if is_paginated%}
,因为它不返回任何值,这就是为什么您看不到任何数字的原因(不要忘记删除关闭的if
{%endif%}

您可以从
页面\u obj

{% for post in page_obj %}
    {{ post }}
{% endfor %}
下面是HTML及其更改

{% for post in page_obj %}
    {{ post.text }}<br>
{% endfor %}


<nav class="blog-pagination justify-content-center d-flex">
    <ul class="pagination">
        {% if page_obj.has_previous %}
        <li class="page-item">
            <a href="?page={{ page_obj.previous_page_number }}" class="page-link" aria-label="Previous">
                <span aria-hidden="true">
                    <span class="ti-arrow-left"></span>
                </span>
            </a>
        </li>
        {% endif %}

    {% for num in page_obj.paginator.page_range %}
        {% if page_obj.number == num %}
        <li class="page-item active">
            <a href="?page={{ num }}" class="page-link">{{ num }}</a>
        </li>
        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
        <li class="page-item">
            <a href="?page={{ num }}" class="page-link">{{ num }}</a>
        </li>
        {% endif %}
    {% endfor %}

    {% if page_obj.has_next %}
        <li class="page-item">
            <a href="?page={{ page_obj.next_page_number }}" class="page-link" aria-label="Next">
                <span aria-hidden="true">
                    <span class="ti-arrow-right"></span>
                </span>
            </a>
        </li>
    {% endif %}
    </ul>
  </nav>
{%用于第_obj%页中的帖子]
{{post.text}}
{%endfor%}
    {%如果页面_obj.has_previous%}
  • {%endif%} {page_obj.paginator.page_range%} {%if page_obj.number==num%}
  • {%elif num>page_obj.number | add:'-3'和num {%endif%} {%endfor%} {%如果页面_obj.has_next%}
  • {%endif%}

什么不起作用,您面临什么问题?它不会在我的页面上显示分页。我的意思是,它没有显示页面的数量,它在一个页面上呈现我的所有帖子。我试图删除if语句,但也不起作用,我没有想过使用for进行迭代,我现在尝试了,但仍然不起作用。你能用所做的更改更新你的HTML模板吗?我让它在我的测试项目中工作。具体如何?我的意思是,当我保存更改并重新启动服务器时,html文件不是由他自己更新的吗?我真的被卡住了,因为我找不到任何错误,这个更新我该怎么做?我用HTML模板代码更新了我的答案(做了更改)。试试看,告诉我你是否能看到页码。哦。。现在我明白了问题所在。我在一个for中呈现帖子,就像这样{%for post in posts%},从第一次开始我就不明白for循环是什么意思,在那之后我下载并更改了for循环,现在一切都很完美。非常感谢。
{% for post in page_obj %}
    {{ post.text }}<br>
{% endfor %}


<nav class="blog-pagination justify-content-center d-flex">
    <ul class="pagination">
        {% if page_obj.has_previous %}
        <li class="page-item">
            <a href="?page={{ page_obj.previous_page_number }}" class="page-link" aria-label="Previous">
                <span aria-hidden="true">
                    <span class="ti-arrow-left"></span>
                </span>
            </a>
        </li>
        {% endif %}

    {% for num in page_obj.paginator.page_range %}
        {% if page_obj.number == num %}
        <li class="page-item active">
            <a href="?page={{ num }}" class="page-link">{{ num }}</a>
        </li>
        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
        <li class="page-item">
            <a href="?page={{ num }}" class="page-link">{{ num }}</a>
        </li>
        {% endif %}
    {% endfor %}

    {% if page_obj.has_next %}
        <li class="page-item">
            <a href="?page={{ page_obj.next_page_number }}" class="page-link" aria-label="Next">
                <span aria-hidden="true">
                    <span class="ti-arrow-right"></span>
                </span>
            </a>
        </li>
    {% endif %}
    </ul>
  </nav>