4 ms·
I've only prototyped adding HTMX to my Django app (currently using React & DRF), but there seems to be a great alignment between Django and HTMX. HTMX wants you
by theptip 5y ago
I've only prototyped adding HTMX to my Django app (currently using React & DRF), but there seems to be a great alignment between Django and HTMX. HTMX wants you to send back a partial HTML tree for the node being updated, and Django's template inheritance machinery lets you very cleanly separate the "parent page" from "partial sub-tree" (since you can just say `{%
include "foo_table_fragment.html" %}` in the parent page template, and then render the "foo_table_fragment.html" template directly on the GET/POST with HTMX headers).
It's all very easy to wire in to a normal Django page to gradually add interactivity (say, partial updates to filter/sort a table to instead of full-page reload).
- number6 5y agoCan you elaborate on the render fragment on GET/POST part? That sounds interesting!
- brylie 5y agoSee the following video tutorial starting at 9:18 for an example of rendering a template fragment in a Django view that gets returned to HTMX https://youtu.be/H_m1g8XOtHY https://youtu.be/H_m1g8XOtHY
- theptip 5y agoIf you use django-htmx, it's pretty simple to get the "this HTTP request came from HTMX" flag and branch accordingly in your view. The docs give a simple example of the logic here: https://django-htmx.readthedocs.io/en/latest/middleware.html#django_htmx.middleware.HtmxDetails https://django-htmx.readthedocs.io/en/latest/middleware.html... The examples give a fleshed-out version: https://github.com/adamchainz/django-htmx/blob/main/example/example/core/views.py#L107-L127 https://github.com/adamchainz/django-htmx/blob/main/example/... Though that's very slightly different than how I described it above, but it's basically the same idea; in this case you commonize the `main` block between the two contexts.
- speg 5y agoThis is what I do (not Django but same idea). Works lovely. Between that and a 900+ package npm install for a simple demo code I’ve given up on “modern JS frameworks”. Or maybe I just got old!