Extensions for using Django with htmx.
Project description
Extensions for using Django with htmx.
Requirements
Python 3.6 to 3.9 supported.
Django 2.2 to 3.2 supported.
Are your tests slow? Check out my book Speed Up Your Django Tests which covers loads of best practices so you can write faster, more accurate tests.
Installation
Install with pip:
python -m pip install django-htmx
Add the middleware:
MIDDLEWARE = [ ..., "django_htmx.middleware.HtmxMiddleware", ..., ]
Example app
See the example app in the example/ directory of the GitHub repository for usage of django-htmx.
API
django_htmx.middleware.HtmxMiddleware
This middleware attaches request.htmx, an instance of HtmxDetails.
django_htmx.middleware.HtmxDetails
This class provides shortcuts for reading the htmx-specific request headers.
__bool__(): bool
True if the request was made with htmx, otherwise False. This is based on the presence of the HX-Request header.
This allows you to switch behaviour for requests made with htmx like so:
def my_view(request):
if request.htmx:
template_name = "partial.html"
else:
template_name = "complete.html"
return render(template_name, ...)
current_url: str | None
The current URL of the browser, or None for non-htmx requests. Based on the HX-Current-URL header.
history_restore_request: bool
True if the request is for history restoration after a miss in the local history cache. Based on the HX-History-Restore-Request header.
prompt: str | None
The user response to hx-prompt if it was used, or None.
target: str | None
The id of the target element if it exists, or None. Based on the HX-Target header.
trigger: str | None
The id of the triggered element if it exists, or None. Based on the HX-Trigger header.
trigger_name: str | None
The name of the triggered element if it exists, or None. Based on the HX-Trigger-Name header.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-htmx-1.1.0.tar.gz
.
File metadata
- Download URL: django-htmx-1.1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 060b07c8cecedd2ecaa992b5c693535373778d57cb8386631cea9cc152502d26 |
|
MD5 | 71e5586c4b3e46be7d63da0f627c4832 |
|
BLAKE2b-256 | bd5828402b5074d16a5dc4e4d539641cdbea78ac751180be3a40ca285c5331e0 |
File details
Details for the file django_htmx-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: django_htmx-1.1.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b29fda9c407992d562aff25c4e79727ca2791f2bdf676172087286450431ca0e |
|
MD5 | 2eaf8cf05e58dd738500b21c95f60b17 |
|
BLAKE2b-256 | bfb0fcbe30da60d3f3afc60bdac0d7671068a349ff8c545290d733363111846c |