【问题标题】:How to create pagination in Django?如何在 Django 中创建分页?
【发布时间】:2015-06-25 02:22:39
【问题描述】:

我关注 djangoproject.com 的这个文档:https://docs.djangoproject.com/en/1.8/topics/pagination/。但这太简单了。它只是下一个和上一个按钮。

现在我想创建具有更多功能的分页,例如http://i.imgur.com/ZiFeAqG.jpg

这是代码:

查看.py

def hire(request):

hire_article_list = hire_article.objects.all().order_by('-id')
#hire_article_list = hire_article.objects.order_by("-publication_date")

paginator = Paginator(hire_article_list, 2) # Show 25 contacts per page

page = request.GET.get('page')
try:
    hire_article_s = paginator.page(page)

except PageNotAnInteger:
    # If page is not an integer, deliver first page.
    hire_article_s = paginator.page(1)
except EmptyPage:
    # If page is out of range (e.g. 9999), deliver last page of results.

    hire_article_s = paginator.page(paginator.num_pages)

#return render_to_response('hire/list.html', {"page_list": page_list})
context = {'hire_article_s': hire_article_s}
return render(request, 'hire/list.html', context)

list.html

{% for j in hire_article_s %}
    {# Each "j" is a page_list  model object. #}
    <li><a href="/hire/{{ j.slug }}-{{j.id}}">{{ j.hiring}}</a></li>
{% endfor %}

                {% if hire_article_s.has_previous %}
                    <a href="?page={{ hire_article_s.previous_page_number }}">previous</a>
                {% endif %}

                <span class="current">
                    Page {{ hire_article_s.number }} of {{ hire_article_s.paginator.num_pages }}.
                </span>

                {% if hire_article_s.has_next %}
                    <a href="?page={{ hire_article_s.next_page_number }}">next</a>
                {% endif %}

        </span>
</div>

【问题讨论】:

  • 能否请您发布您尝试使用更多功能进行分页的代码。
  • 感谢您的回复。我加了
  • 如果这不起作用,请尝试使用 django 无限分页库
  • 但我不想使用 djangopackage。只是python代码。对我来说似乎很难找到解决方案

标签: django pagination


【解决方案1】:

上周我也有类似的需求,发现这个超级有用的 gist (https://gist.github.com/jsatt/8183993) 运行良好(尽管我不确定为什么在我将请求放入函数参数之前它不起作用)。它是 django 的 Paginator 函数的子类。你可以把它放在一个实用程序文件中,并在你想在范围内使用 Paginate 时调用它。

例如,我的核心应用程序中有一个名为 utils.py 的文件。

views.py

from core.utils import paginate

def hire(request):

    hire_article_list = hire_article.objects.all().order_by('-id')
    '''
    Show 25 contacts per page, with a page range of 5, which means if you are
    on page 8, it shows links to pages 6,7,8,9,10.
    '''
    hire_article_s = paginate(request, hire_article_list, 25, 5) 

    context = {'hire_article_s': hire_article_s}
    return render(request, 'hire/list.html', context)

list.html

{% if hire_article_s.has_previous %}
     <a href="?page={{ hire_article_s.previous_page_number }}">previous</a>
{% endif %}

{% for range in hire_article_s.neighbor_range %}
    {% if range == hire_article_s.number %}
       <li class="pagination__item active ">{{ range }}</li>
    {% else %}
       <li class="{% if range == hire_article_s.number %}active {% endif %}"><a href="?page={{ range }}">{{ range }}</a></li>
    {% endif %}
{% endfor %} 

{% if hire_article_s.has_next %}
    <a href="?page={{ hire_article_s.next_page_number }}">next</a>
{% endif %}

希望这会有所帮助。

更新

上面的代码做了一些修改。我添加了上下文和模板格式。请注意,我使用循环遍历{{ hire_article_s.neighbor_range }} 并打印出页码。我还检查以突出显示当前页面的编号。这应该可以工作,因为它几乎是我自己的代码和你自己的变量名。

【讨论】:

  • 谢谢,但是当我尝试但没有工作时。你能把你的代码发给我吗?
  • @user3863069 我已经更新了代码并进行了一些额外的说明。如果这不起作用,您需要告诉我您遇到了什么错误。
  • 输出的样子
  • 上一个 下一个 | 2 | 3 | 4 | 5 | 6 | 7 当我点击数字时,什么也没发生\
  • 它正在生成页码,这意味着它可能只是 HTML 标记中的错误。你能发布生成的html吗?
猜你喜欢
  • 2021-06-20
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 2010-11-22
  • 1970-01-01
  • 2016-12-31
相关资源
最近更新 更多