Simple package that facilitates the usage of HTMX with Django
Project description
simple-django-htmx
Overview
A package to simplify the usage of HTMX with Django. Easily add HTMX reuqests witout needing additional urls, and reduce clutter in views by offloading all responsibility to an hx_request.
Why use?
- Avoid cluttering up urls with endpoints for HTMX
- Reduce clutter in views by avoiding if/elses that are checking if the incoming request is an HTMX request
- Built in functionality for HTMX requests that handle forms
How to use
-
Installation
pip install simple-django-htmx
- Add simple_django_htmx to your list of installed apps
- You will also need to have HTMX installed. See https://htmx.org/
-
Quick Example
-
Create an HXRequest:
class EditUserHxRequest(FormHXRequest): name = "edit_user" GET_template = "update_user.html" POST_template = "user_row.html" form_class = EditUserForm
-
Create the template where this HXRequest will be used. Notice how the template tag is used to render the HXRequest.
user_list.html
{% load hx_tags %} <tr > <td> {{user.name}} </td> <td> {{user.email}} </td> <td> {{user.address}} </td> <td> <button {% render_hx 'edit_user' 'get' object=user %} hx-target="closest tr" hx-swap="outerHTML">Edit</button> </td> </tr>
-
Load the HXRequest into the view that it's being used in. The view needs to inherit from HtmxViewMixin and the view needs to provide a list of hx_requests that will be used in the view.
class UserListView(HtmxVIewMixin, ListView): template_name = "user_list.html" model = User hx_requests = [ EditUserHxRequest ]
-
Voila! on the click of the Edit button the GET_template from EditUserHxRequest will be loaded.
- The view is neat and clean and there was no need to create a url for the edit button to hit.
A Deeper Dive
---HXRequest Classes---
HXRequest
A wrapper for HTMX requests. Instead of the GET/POST getting dircted to a view, it goes to an HXRequest.
- Attributes
- name: Unique string that is used to identify which HXRequest the HTMX reuqest should direct to
- GET_template: The template that an hx-get fetches
- POST_template: The template an hx-post fetches
- hx_object_name: Default is hx_object. If an object is passed into the
render_hx
template tag the obejct is put into the context of the GET/POST templates. The hx_object_name is the key in the contedxt for this object.
- Methods
- get_context_data: Same as a view's get_context_data. HXRequest's add in a few additional items into the context.
- hx_kwargs -> These are any kwargs passed into
render_hx
- hx_object (or the name given to it using
hx_object_name
) -> The object passed intorender_hx
. In theFormHXRequest
this object acts like the object of a Django UpdateView. - hx_request: The HXRequest
- hx_kwargs -> These are any kwargs passed into
- get_context_data: Same as a view's get_context_data. HXRequest's add in a few additional items into the context.
FormHXRequest
Acts like a a Django UpdateView. When a form is valid it saves the form and by default returns the POST_template of the HXRequest. If the form is invalid it returns the GET_temlate by default. Can customize what the view returns by overriding form_valid
or form_invalid
and return anything from those functions.
- Attributes
- form_class: This is passed into the context as form and is instantiated with the object passed into
render_hx
. On POST it is instantiated with the request.POST as well.
- form_class: This is passed into the context as form and is instantiated with the object passed into
-
Methods
- form_valid: Method called when the form is valid. By default it just calls
form.save()
. Has access to kwargs sent in through therender_hx
template tag. - form_invalid: Method called when the form is invalid. No default. Has access to kwargs sent in through the
render_hx
template tag. - get_form_kwargs: Injects kwargs into the form. Can override to put items into the form. i.e. to set initial values. Has access to kwargs sent in through the
render_hx
template tag.
- form_valid: Method called when the form is valid. By default it just calls
DeleteHXRequest
Deletes the object passed into render_hx
. Can override handle_delete
for custom functionality.
---Other---
HtmxVIewMixin -> The mixin intercepts the GET and POST and if it finds hx_request in the GET params it redircts to the HXRequest's GET and POST methods.
render_hx -> Template tag to use for HXReuqests. Takes in:
- Name of the HXReqest
- Type of reuqest, get or post
- object (optional), the object that is used by the request. It is treated just like the object of an UpdateView
- kwargs, addional params that can be passed in. These kwargs can be used in get_form_kwargs, form_valid, of form_invalid.
-
Example -> The template has a user and is looping through the contact methods of the user. There is an HXRequest on each contact method that lets you edit the contact method. Would need to pass in the contact method as the object. Addionally the page has an add new button which allows you to add a new contact method to this user, you would need to pass an addional kwarg for the user because when saving the new contact method, the back end needs to know which user we are saving this for.
<button {% hx_request_name='render_hx create_update_contact_method' method="get" object="contact_method" user=user %} hx-target="closest tr" ></button>
-
Future Features
- Aysnchronous messaging.
- Auto filling initial of form fields with kwargs if the kwarg key matches the form field.
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
Built Distribution
File details
Details for the file simple-django-htmx-1.0.1.tar.gz
.
File metadata
- Download URL: simple-django-htmx-1.0.1.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.15.0-53-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 501e3f8c11e4aa28efc3e3ea78d94c8cd773beb2075231727add16cfb30cc771 |
|
MD5 | c3716c00a8dd016b6dfb1dba443a7651 |
|
BLAKE2b-256 | a425fee6f8868c219d8ac2de8b9aacb6240cab1af3b8702c6e148a6b4bbad416 |
File details
Details for the file simple_django_htmx-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: simple_django_htmx-1.0.1-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.15.0-53-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d18f397c1f352d9be8a056c46ea30ca5218f17b6d0d380d19780432cc29e293 |
|
MD5 | 93dcfc8262972394fefc7c8ca9dc359d |
|
BLAKE2b-256 | cd246c6fcae82fbbea28533d2462a1379398a37da93e9cbb5e4327e3631b8c00 |