Learn Python with Talk Python's 270 hours of courses

HTMX for Django Developers (And All of Us)

Episode #437, published Tue, Nov 7, 2023, recorded Thu, Oct 5, 2023

Are you considering or struggling with replacing much of the interactivity of your Django app with frontend JavaScript frameworks? After all, your users do expect an interactive and modern app, right? Before you make a rash decision, you owe it to yourself to check out HTMX. It goes well with Django. We have Christopher Trudeau to run through a whole awesome list of HTMX and Python and tell us about his new HTMX + Django course.

Watch this episode on YouTube
Play on YouTube
Watch the live stream version

Episode Deep Dive

Guests Introduction and Background

Christopher Trudeau joins the show as an experienced Python developer and a prolific course author who has created over a dozen Python courses, including a new one called HTMX + Django. He is also the author of the in-progress book Django in Action, which offers detailed coverage of Django, integrating modern approaches, and third-party libraries. Christopher shares a passion for cleaner, simpler ways to build interactive web apps, specifically highlighting the growing appeal of HTMX for Django projects.

What to Know If You're New to Python

If you are just starting with Python and want to get the most out of this episode, it helps to know some basic web concepts:

  • Having a rough understanding of Django and its views (functions or classes) will make many references clearer.
  • Knowing how HTML requests and server responses work (the traditional request-response cycle) is key to seeing how HTMX changes the workflow.
  • Being comfortable with basic Python data structures and functions will help in following the examples described.

Key Points and Takeaways

  1. Django and HTMX: A Powerful Synergy
    Django provides an all-in-one framework for building robust web apps. HTMX complements Django by adding interactivity without needing heavy JavaScript frameworks like React or Vue. By merging server-rendered HTML with HTMX attributes, developers can create dynamic, modern-feeling applications while staying in Python.

  2. Minimal Front-End Code, Maximum Interactivity
    HTMX focuses on extending HTML elements with attributes such as hx-get and hx-post to handle “AJAX-like” requests invisibly. This radically reduces JavaScript, letting developers stay in Python and Django templates for interactivity. The result is shorter build times, fewer dependencies, and code that’s easier to maintain.

  3. Partials and Template Reuse
    One of the practical challenges is reusing HTML snippets for dynamic sections of a page. The discussion covers two main styles: having a dedicated partial file for snippets or using a single template block that is selectively rendered. Both approaches ensure no duplicated markup while delivering the specific HTML fragment required by HTMX calls.

  4. Case Study: Dramatic Code Reduction Moving from React
    Christopher references an HTMX case study on the HTMX.org site where a team replaced a React front-end with HTMX and cut their codebase by 67%. They also significantly reduced dependencies and made their Django app more performant. This underscores how adopting HTMX can enhance both simplicity and speed.

  5. Server-Sent Events (SSE) and Polling
    While many modern apps use WebSockets, HTMX can leverage built-in polling to simulate live updates. For real-time streams, SSE (Server-Sent Events) can also integrate with frameworks such as Starlette or FastAPI. These tools minimize front-end overhead by offloading more logic to Python on the server side.

  6. The django-htmx Library
    Created by Adam Johnson, django-htmx is one of the go-to packages for seamless HTMX integration in Django. It adds convenient middleware and request features (like request.htmx) so developers can detect whether a request is from HTMX and return partial HTML or a full page accordingly.

  7. Practical Patterns and Examples
    The conversation highlights ways to structure Django views to handle both normal requests and HTMX calls in a single function. For deeper guidance, Luke Plant’s “Django HTMX Patterns” is recommended. This covers everything from form handling to small updates like inline editing and endless scrolling.

  8. A Wella Club: End-to-End Example
    Adam Johnson’s “A Wella Club” board game project is an in-depth, fully working Django + HTMX app that demonstrates everything from partial rendering to polling. It shows how advanced interactivity (including computer-vs-player logic) can live mostly on the server, with minimal front-end code.

Interesting Quotes and Stories

  • “HTMX is really the answer; I’d love to see it become part of the HTML standard.”
    Christopher’s enthusiastic endorsement of HTMX for simplifying web development.
  • “It’s not a dinosaur, it’s a shark.”
    A metaphor Christopher shared about Django’s maturity: it remains vibrant and constantly evolving rather than being stuck in the past.
  • “React is overkill for me.”
    A sentiment expressed about why the HTMX approach, especially for typical form-based apps, can be more efficient and cleaner than heavy JavaScript frameworks.

Key Definitions and Terms

  • HTMX: A lightweight JavaScript library that extends HTML elements with attributes like hx-get, hx-post, etc. It enables partial page updates without rewriting large parts of the front end.
  • Partials: Reusable fragments of HTML that can be rendered separately, commonly used in dynamic sections of a page in Django or Jinja2 templates.
  • Server-Sent Events (SSE): A technology to send automatic server updates to the client over HTTP, often used for dashboards or live data streams.
  • Function-Based Views: A Django approach where each view is a simple function, contrasting with class-based views which rely on a class structure for routing requests.

Learning Resources

Here are a few resources to dive deeper into Django and HTMX:

Overall Takeaway

Django and HTMX are a potent combination, offering modern web interactivity without the bloat and complexity of heavy JavaScript frameworks. By using partials, simple request handlers, and a healthy ecosystem of tools like django-htmx, developers can build rich experiences, reduce overall code, and keep their projects firmly in Python land. If you’re a Django user looking to push beyond classic server-rendered pages, HTMX may be the secret ingredient to a cleaner, faster, and more maintainable application.

Links from the show

Chris on ExTwitter: @cltrudeau
Django in Action book: manning.com
Django: djangoproject.com
HTMX + Django course: talkpython.fm
HTMX: htmx.org
awesome-htmx: github.com
awesome-python-htmx: github.com
django-js-lib-htmx: github.com
htmxflask: github.com
fastapi-sse-htmx: github.com
django-htmx-patterns: github.com
jinja2-fragments: github.com
jinja_partials: github.com
chameleon_partials: github.com
django-render-block: github.com
flask-htmx: github.com
htmx-flask: github.com
asgi-htmx: github.com
hx-requests: github.com
django-dashboards: github.com
A Real World React -> htmx Port: htmx.org
3 IRL use cases for Python and HTMX: bitecode.dev
owela-club: github.com
Watch this episode on YouTube: youtube.com
Episode transcripts: talkpython.fm

--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy

Talk Python's Mastodon Michael Kennedy's Mastodon