I cannot reproduce the same appearance of the table pagination format from the basic django-tables2
example. Here is my code
Model:
#models.py
class Person(models.Model):
name = models.CharField(verbose_name="full name", max_length=200)
Table:
# tables.py
import django_tables2 as tables
from loaddata.models import Person
class PersonTable(tables.Table):
class Meta:
model = Person
# add class="paleblue" to <table> tag
attrs = {"class": "paleblue"}
View:
#views.py
from django.shortcuts import render
from django_tables2 import RequestConfig
from loaddata.models import Person
from loaddata.tables import PersonTable
def people(request):
table = PersonTable(Person.objects.all())
RequestConfig(request, paginate={"per_page": 25}).configure(table)
return render(request, "loaddata/people.html", {"table": table})
This code produces the following table (#1
)
Whereas according to the tutorial, the table should look like this (#2
)
As seen my table (#1
) misses the current page number, but instead it shows the total number of data series (2 persons
). However if the pagination per_page
parameter in the view
is changed to 1
, i.e.
View modified:
#views.py
...
RequestConfig(request, paginate={"per_page": 1}).configure(table)
...
then my table (#3
) will show the current paging and the redundant 1 of 2 persons
.
What should I change in the code to remove from the pagination area the total number of data series (get rid of 2 persons
, 1 of 2 persons
) and force the current page numbering be present even if the table has one page (i.e. make table #1
the same as table #2
)?
I am using:
python 3.4.3
django 1.9
django_tables2 1.1.6
If you take a look at the default page template (for instance @ https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/templates/django_tables2/table.html) you'll see the following two blocks:
{% if table.page.has_previous or table.page.has_next %}
{% block pagination.current %}
<li class="current">
{% blocktrans with table.page.number as current and table.paginator.num_pages as total %}Page {{ current }} of {{ total }}{% endblocktrans %}
</li>
{% endblock pagination.current %}
{% endif %}
....
{% block pagination.cardinality %}
<li class="cardinality">
{% if total != count %}{% blocktrans %}{{ count }} of {{ total }}{% endblocktrans %}{% else %}{{ total }}{% endif %} {% if total == 1 %}{{ table.data.verbose_name }}{% else %}{{ table.data.verbose_name_plural }}{% endif %}
</li>
{% endblock pagination.cardinality %}
The first block checks if the table has more than one page an displays the page number - so you should remove the check if you want to always display the page number. The other block displays the amount of items - so you could just dump it.
Now, to override the table template there are two ways:
Override the table template globally for all django-tables2 tables on your site: In on of your apps (or your global templates directory) add a file named django_tables2/table.html
and copy there the table.html from the source of django_tables2 - with the mentioned above edits of course. Then all your tables will use the new template.
Override just one template: Copy the table.html source I mentioned above somewhere with a different name (for example mytable.html
) and then use that template to render your table by passing it ot the template tag, something like {% render table "mytable.html" %}
(https://django-tables2.readthedocs.io/en/latest/pages/template-tags.html#render-table)