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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
Template tags
=============
django-htmx comes with two template tags for rendering ``<script>`` tags, the first of which includes a vendored version of htmx.
The tags are available for both of Django’s built-in template engines:
* For Django templates, use the ``django_htmx`` template library with ``{% load django_htmx %}``.
* For Jinja, import the functions from ``django_htmx.jinja`` and add them to the environment.
All ``<script>`` tags are rendered with |the defer attribute|__ to avoid blocking page rendering.
.. |the defer attribute| replace:: ``defer`` attribute
__ https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#defer
``htmx_script``
---------------
The ``htmx_script`` template tag renders two script tags for:
1. The vendored version of htmx included in django-htmx.
The current vendored version of htmx is 2.0.6.
(`htmx release notes <https://github.com/bigskysoftware/htmx/releases>`__.)
2. django-htmx’s extension script, when |settings.DEBUG|__ is ``True``.
This script adds an error handler for debugging HTTP errors, :ref:`explained below <django-htmx-extension-script>`.
.. |settings.DEBUG| replace:: ``settings.DEBUG``
__ https://docs.djangoproject.com/en/stable/ref/settings/#debug
Django templates
^^^^^^^^^^^^^^^^
Load the library and use ``{% htmx_script %}`` in your ``<head>`` tag, typically in a base template:
.. code-block:: django
:emphasize-lines: 6
{% load django_htmx %}
<!doctype html>
<html>
<head>
...
{% htmx_script %}
</head>
<body>
...
</body>
</html>
The default is to use a minified version of htmx.
Pass ``minified=False`` to render the non-minified version:
.. code-block:: django
{% htmx_script minified=False %}
This may be useful when debugging htmx behaviour.
Jinja
^^^^^
First, load the tag function into the globals of your `custom environment
<https://docs.djangoproject.com/en/stable/topics/templates/#django.template.backends.jinja2.Jinja2>`__:
.. code-block:: python
:emphasize-lines: 10
from jinja2 import Environment
from django_htmx.jinja import htmx_script
def environment(**options):
env = Environment(**options)
env.globals.update(
{
# ...
"htmx_script": htmx_script,
}
)
return env
Second, call the function in a variable in your ``<head>`` tag, typically in a base template:
.. code-block:: jinja
:emphasize-lines: 6
{% load django_htmx %}
<!doctype html>
<html>
<head>
...
{{ htmx_script() }}
</head>
<body>
...
</body>
</html>
The default is to use a minified version of htmx.
Pass ``minified=False`` to render the non-minified version:
.. code-block:: jinja
{{ htmx_script(minified=False) }}
This may be useful when debugging htmx behaviour.
``django_htmx_script``
----------------------
The ``django_htmx_script`` template tag renders a script tag only for the django-htmx extension script (:ref:`explained below <django-htmx-extension-script>`), when ``settings.DEBUG`` is ``True``.
Use it when you’re sourcing htmx from outside django-htmx.
Django templates
^^^^^^^^^^^^^^^^
Load and use the template tag after your htmx ``<script>`` tag:
.. code-block:: django
:emphasize-lines: 7
{% load django_htmx %}
<!doctype html>
<html>
<head>
...
<script src="{% static 'custom/htmx.min.js' %}" defer></script>
{% django_htmx_script %}
</head>
<body>
...
</body>
</html>
Jinja
^^^^^
First, load the tag function into the globals of your `custom environment
<https://docs.djangoproject.com/en/stable/topics/templates/#django.template.backends.jinja2.Jinja2>`__:
.. code-block:: python
:emphasize-lines: 10
from jinja2 import Environment
from django_htmx.jinja import django_htmx_script, htmx_script
def environment(**options):
env = Environment(**options)
env.globals.update(
{
# ...
"django_htmx_script": django_htmx_script,
}
)
return env
Second, call the function in a variable in your ``<head>`` tag, typically in a base template:
.. code-block:: jinja
:emphasize-lines: 7
{% load django_htmx %}
<!doctype html>
<html>
<head>
...
<script src="{{ static('custom/htmx.min.js') }}" defer></script>
{{ django_htmx_script() }}
</head>
<body>
...
</body>
</html>
.. _django-htmx-extension-script:
django-htmx extension script
----------------------------
This script, rendered by either of the above template tags when ``settings.DEBUG`` is ``True``, extends htmx with an error handler.
htmx’s default behaviour when encountering an HTTP error is to discard the response content, which can make it hard to debug errors.
This script adds an error handler that detects responses with 404 and 500 status codes and replaces the page with their content.
This change exposes Django’s default error responses, allowing you to debug as you would for a non-htmx request.
See the script in action in the “Error Demo” section of the :doc:`example project <example_project>`.
See its source `on GitHub <https://github.com/adamchainz/django-htmx/blob/main/src/django_htmx/static/django_htmx/django-htmx.js>`__.
|