File: faq.rst

package info (click to toggle)
django-tables 2.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,752 kB
  • sloc: python: 7,120; makefile: 132; sh: 74
file content (109 lines) | stat: -rw-r--r-- 3,467 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
.. _faq:

..
    Any code examples in this file should have a corresponding test in
    tests/test_faq.py

FAQ
===

Some frequently requested questions/examples. All examples assume you
import django-tables2 like this::

    import django_tables2 as tables

How should I fix error messages about the request context processor?
--------------------------------------------------------------------

The error message looks something like this::

    Tag {% querystring %} requires django.template.context_processors.request to be
    in the template configuration in settings.TEMPLATES[]OPTIONS.context_processors)
    in order for the included template tags to function correctly.

which should be pretty clear, but here is an example template configuration anyway::

    TEMPLATES = [
        {
            "BACKEND": "django.template.backends.django.DjangoTemplates",
            "DIRS": ["templates"],
            "APP_DIRS": True,
            "OPTIONS": {
                "context_processors": [
                    "django.contrib.auth.context_processors.auth",
                    "django.template.context_processors.request",
                    "django.template.context_processors.static",
                ],
            }
        }
    ]

How to create a row counter?
----------------------------

You can use ``itertools.counter`` to add row count to a table. Note that in a
paginated table, every page's counter will start at zero::

    class CountryTable(tables.Table):
        counter = tables.TemplateColumn("{{ row_counter }}")


How to add a footer containing a column total?
----------------------------------------------

Using the `footer`-argument to `~.Column`::

    class CountryTable(tables.Table):
        population = tables.Column(
            footer=lambda table: sum(x["population"] for x in table.data)
        )

Or by creating a custom column::

    class SummingColumn(tables.Column):
        def render_footer(self, bound_column, table):
            return sum(bound_column.accessor.resolve(row) for row in table.data)

    class Table(tables.Table):
        name = tables.Column(footer="Total:")
        population = SummingColumn()

Documentation: :ref:`column-footers`

.. note ::
    Your table template must include a block rendering the table footer!


Can I use inheritance to build Tables that share features?
----------------------------------------------------------

Yes, like this::

    class CountryTable(tables.Table):
        name = tables.Column()
        language = tables.Column()

A `CountryTable` will show columns `name` and `language`::

    class TouristCountryTable(CountryTable):
        tourist_info = tables.Column()

A `TouristCountryTable` will show columns `name`, `language` and `tourist_info`.

Overwriting a `Column` attribute from the base class with anything that is not a
`Column` will result in removing that Column from the `Table`. For example::

    class SimpleCountryTable(CountryTable):
        language = None

A `SimpleCountryTable` will only show column `name`.


How can I use with Jinja2 template?
-----------------------------------

In Jinja2 templates, the ``{% render_table %}`` tag is not available, but you can still use *django-tables2* like this::

    {{ table.as_html(request) }}

where ``request`` need to be passed from view, or from *context processors* (which is supported by `django-jinja <https://niwinz.github.io/django-jinja/latest/#_context_processors_support>`_).