Skip to main content

Extensions for using Django with htmx.

Project description

https://img.shields.io/github/workflow/status/adamchainz/django-htmx/CI/main?style=for-the-badge https://img.shields.io/coveralls/github/adamchainz/django-htmx/main?style=for-the-badge https://img.shields.io/pypi/v/django-htmx.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

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

  1. Install with pip:

    python -m pip install django-htmx
  2. 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: 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-htmx-1.0.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

django_htmx-1.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file django-htmx-1.0.1.tar.gz.

File metadata

  • Download URL: django-htmx-1.0.1.tar.gz
  • Upload date:
  • Size: 13.5 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

Hashes for django-htmx-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2bdcfa77301f352a9433f3ab4921649ed6c3814c79b627829a9a9409a191a104
MD5 bd08742a50effa92872ed943855a24bd
BLAKE2b-256 e506530451c162ef063ac071cf0bac4ba128528a581877e17c9462133b38e989

See more details on using hashes here.

File details

Details for the file django_htmx-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_htmx-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 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

Hashes for django_htmx-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 28920b8144e2152f7fc476d2a49cb70057da8f04b29024f52ee3a85590ebd39b
MD5 6dc7936c6c91894cc149f6dd489adbb5
BLAKE2b-256 76bbfabaa372284144cb00df2c1368c48c4e01af5534cb34e6d59f8af5e52ed0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page