用django-tables2显示页码 [英] Displaying Page Numbers with django-tables2
问题描述
我正在使用django-tables2显示数据集。
I'm currently displaying a dataset using django-tables2.
文档特别没有提及,所以我猜这可能会一些表格超越 - 但是,我希望有人在这里已经完成了这一点。
The docs make no mention of this in particular, so I'm guessing this'll take probably some table overriding - but, I'm hopeful someone out there has already accomplished this.
如何使用表下面的django-tables2来呈现页面编号?我想要显示的是用户可以点击的页面编号的横向列表。
How can I render page numbers using django-tables2 below my table? What I'd like to be able to display is a horizontal list of page numbers that the user can click.
提前感谢。
推荐答案
您需要创建自定义页面呈现模板 - 您不需要覆盖任何类。
you need to create a custom page rendering template - you don't need to override any classses.
要做到这一点,首先复制文件
To do that, start by copying the file
PYTHON\Lib\site-packages\django_tables2\templates\django_tables2\table.html
到模板您的django应用程序中的
目录,并将其重命名为 mytable.html
或其他任何您喜欢的。
to the templates
directory inside your django application and rename it to mytable.html
or whatever else you like.
现在,您需要更改该文件的分页块。有很多方法可以做你喜欢的事情,但一种简单的方法是在分页块中添加以下行(您可以根据具体需要删除或保留其他内容):
Now, you need to change the pagination block of that file. There are many ways to do what you like, but a simple way is to add the following lines inside the pagination block (you may remove or keep the other things that are there depending on your specific needs):
{% block pagination.allpages %}
{% for p in table.paginator.page_range %}
<a href="{% querystring table.prefixed_page_field=p %}">{{ p }}</a>
{% endfor %}
{% endblock pagination.allpages %}
最后,使用您的模板,只需将自定义模板名称传递给render_table命令:
Finally, to use your template, just pass your custom template name to the render_table command:
{% load render_table from django_tables2 %}
...
{% render_table table "mytable.html" %}
这很简单,如果你有很多页面,你会有麻烦使用一些如果
s通过 table.paginator.num_pages
变量检查页数。此外,您可以突出显示当前页面,并使用 table.page.number
变量禁用链接。
This is very simple and will give you trouble if you have many pages (so you have to use some if
s to check the number of pages through the table.paginator.num_pages
variable). Also, you may highlight the current page and disable the link by using the table.page.number
variable.
以上内容为读者留下了遗憾:)
The above are left as an excersise to the reader :)
这篇关于用django-tables2显示页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!