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
The middleware does a few things:
It attaches request.htmx, an instance of HtmxDetails (see below).
It modifies request.method based upon the X-HTTP-Method-Override header. htmx sends this header to set what the method “really is” since the JavaScript XMLHttpRequest API only allows sending GET and POST requests.
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: Optional[str]
The current URL of the browser, or None for non-htmx requests. Based on the HX-Current-URL header.
prompt: Optional[str]
The user response to hx-prompt if it was used, or None.
target: Optional[str]
The id of the target element if it exists, or None. Based on the HX-Target header.
trigger: Optional[str]
The id of the triggered element if it exists, or None. Based on the HX-Trigger header.
trigger_name: Optional[str]
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.0.0.tar.gz
.
File metadata
- Download URL: django-htmx-1.0.0.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2bf0c2361aef433839602c3d72ddb8adfbaa6d0b9306b5b514630b811d48754 |
|
MD5 | a8c7c7f26d513a1c8d5d702807b3b957 |
|
BLAKE2b-256 | ff9b748577524874effdba42fda14453efca9237db76336d763b8ddd89b507c8 |
File details
Details for the file django_htmx-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: django_htmx-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb8c9fc329cf6281bd78c563543253c52101733e5f32eca0fb289baf4ec06571 |
|
MD5 | 241f1e6165459fc02b0e2100e1cc1588 |
|
BLAKE2b-256 | 594cab75fd1901d2d22e8ad9c09d398307ba1d050229ca491d37fa205409125e |