pgryff / django-htmx

Extensions for using Django with htmx.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

django-htmx

image

image

image

image

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: str | None

The current URL of the browser, or None for non-htmx requests. Based on the HX-Current-URL 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.

About

Extensions for using Django with htmx.

License:MIT License


Languages

Language:Python 43.3%Language:CSS 28.6%Language:HTML 28.2%