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.
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.
Thanks in advance.
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
to the
templates
directory inside your django application and rename it tomytable.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):
Finally, to use your template, just pass your custom template name to the render_table command:
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 thetable.paginator.num_pages
variable). Also, you may highlight the current page and disable the link by using thetable.page.number
variable.The above are left as an excersise to the reader :)
Improving on @Serafeim answer (or solving the exercise he left): Here is a pagination block which, using only Django template syntax, renders page numbers that:
<ul>
HTML block, whith CSS classes that "play well" with Bootstrap;first and last pages are always shown, with ellipsis between them and the start or end of the range (if needed).
Pagination is introduced in version# >= 2.0.0 https://django-tables2.readthedocs.io/en/latest/pages/CHANGELOG.html
Simply add following code in settings.py. Pagination with number will be rendered with bootstap 4 style. Make sure you have bootstrap 4 reference in html template.
And check out more styles in documentation. https://django-tables2.readthedocs.io/en/latest/pages/custom-rendering.html#available-templates