| 1 |
================ |
| 2 |
Widget Reference |
| 3 |
================ |
| 4 |
|
| 5 |
This is a reference document with a list of the provided widgets and their |
| 6 |
arguments. |
| 7 |
|
| 8 |
|
| 9 |
.. _link-widget: |
| 10 |
|
| 11 |
``LinkWidget`` |
| 12 |
~~~~~~~~~~~~~~ |
| 13 |
|
| 14 |
This widget renders each option as a link, instead of an actual <input>. It has |
| 15 |
one method that you can override for additional customizability. |
| 16 |
``option_string()`` should return a string with 3 Python keyword argument |
| 17 |
placeholders: |
| 18 |
|
| 19 |
1. ``attrs``: This is a string with all the attributes that will be on the |
| 20 |
final ``<a>`` tag. |
| 21 |
2. ``query_string``: This is the query string for use in the ``href`` |
| 22 |
option on the ``<a>`` element. |
| 23 |
3. ``label``: This is the text to be displayed to the user. |
| 24 |
|
| 25 |
|
| 26 |
.. _boolean-widget: |
| 27 |
|
| 28 |
``BooleanWidget`` |
| 29 |
~~~~~~~~~~~~~~~~~ |
| 30 |
|
| 31 |
This widget converts its input into Python's True/False values. It will convert |
| 32 |
all case variations of ``True`` and ``False`` into the internal Python values. |
| 33 |
To use it, pass this into the ``widgets`` argument of the ``BooleanFilter``: |
| 34 |
|
| 35 |
.. code-block:: python |
| 36 |
|
| 37 |
active = BooleanFilter(widget=BooleanWidget()) |
| 38 |
|
| 39 |
|
| 40 |
.. _csv-widget: |
| 41 |
|
| 42 |
``CSVWidget`` |
| 43 |
~~~~~~~~~~~~~ |
| 44 |
|
| 45 |
This widget expects a comma separated value and converts it into a list of |
| 46 |
string values. It is expected that the field class handle a list of values as |
| 47 |
well as type conversion. |
| 48 |
|
| 49 |
|
| 50 |
.. _range-widget: |
| 51 |
|
| 52 |
``RangeWidget`` |
| 53 |
~~~~~~~~~~~~~~~ |
| 54 |
|
| 55 |
This widget is used with ``RangeFilter`` and its subclasses. It generates two |
| 56 |
form input elements which generally act as start/end values in a range. |
| 57 |
Under the hood, it is Django's ``forms.TextInput`` widget and accepts |
| 58 |
the same arguments and values. To use it, pass it to ``widget`` argument of |
| 59 |
a ``RangeField``: |
| 60 |
|
| 61 |
.. code-block:: python |
| 62 |
|
| 63 |
date_range = DateFromToRangeFilter(widget=RangeWidget(attrs={'placeholder': 'YYYY/MM/DD'})) |
| 64 |
|
| 65 |
|
| 66 |
``SuffixedMultiWidget`` |
| 67 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 68 |
|
| 69 |
Extends Django's builtin ``MultiWidget`` to append custom suffixes instead of |
| 70 |
indices. For example, take a range widget that accepts minimum and maximum |
| 71 |
bounds. By default, the resulting query params would look like the following: |
| 72 |
|
| 73 |
.. code-block:: http |
| 74 |
|
| 75 |
GET /products?price_0=10&price_1=25 HTTP/1.1 |
| 76 |
|
| 77 |
By using ``SuffixedMultiWidget`` instead, you can provide human-friendly suffixes. |
| 78 |
|
| 79 |
.. code-block:: python |
| 80 |
|
| 81 |
class RangeWidget(SuffixedMultiWidget): |
| 82 |
suffixes = ['min', 'max'] |
| 83 |
|
| 84 |
The query names are now a little more ergonomic. |
| 85 |
|
| 86 |
.. code-block:: http |
| 87 |
|
| 88 |
GET /products?price_min=10&price_max=25 HTTP/1.1 |