rx-36

Creating a modal window with Django, HTMX and hyperscript

Note: On wide screens, code blocks may appear beside the explanatory text; on narrower screens (tablet/mobile) they appear below. Instructions like “add the code” refer to the adjacent code block regardless of placement.

The SPA Dilemma: Django vs. JavaScript Frameworks

In the world of web development, building rich, interactive applications using JavaScript frameworks like React or Vue has become increasingly common. Many new projects start by choosing a front-end framework. Developers accustomed to these frameworks might find Django feels somewhat old-fashioned. However, for those of us who've been using Django since before the rise of modern JavaScript, learning React, Vue, or Angular from scratch can feel like a tedious hurdle.

If you're one such developer, you might wonder if a full front-end framework is really necessary to build an SPA. Yes—you can stitch together Django templates with CSS, vanilla JavaScript, and AJAX to make it work, but the codebase tends to become complex and hard to maintain as interactions grow.

Instead of layering ad‑hoc scripts, HTMX lets you express interactivity declaratively in your HTML while keeping your Django project cohesive and well-structured. For many Django apps, it’s the cleanest, most maintainable path to SPA‑like behavior—without adopting a heavy framework.

HTMX is gaining popularity among Django developers as a tool for creating SPA-like experiences without adopting a full JavaScript framework. Essentially, HTMX acts as a drop-in replacement for AJAX, allowing you to build dynamic and interactive interfaces while working primarily in Django. This empowers you to deliver modern web experiences to users without the complexities of newer frameworks.

In this post, I'll show you how to use Django, HTMX, and Hyperscript to create a modal window—a common component in modern web applications. This combination can help you enhance your Django projects without needing to write complex JavaScript code or dive into a full front-end framework. Let's get started!

Introducing HTMX

HTMX is a JavaScript library that allows you to handle dynamic events and asynchronous communication simply by adding attributes to your HTML, without writing JavaScript. Unlike traditional AJAX, which returns JSON data, HTMX handles asynchronous server communication by returning HTML itself. This means you can directly update portions of your page with server-rendered HTML. This approach empowers developers to deliver the interactive functionality users expect from modern web applications while maintaining a cleaner and more understandable codebase.

Application Overview

Development Process

Django packages

1. Setting up the Django Project

2. Integrating Hyperscript with HTMX and base.html

3.Defining the Data Model (models.py)

4.Creating the Django Form (forms.py)

5. Registering Models in the Admin Interface (admin.py)

6. Implementing the HomeView (views.py)

7. Designing the Home Page Template (home.html)

8. Configuring URL Routing (urls.py)

9. Creating the Modal View (views.py)

10. Designing the Modal Template (modal.html)

10-1. Hyperscript

10-2. htmx

10-3. Creating View

11. Creating the Comment List Template (comment-list.html) and Updating home.html

12. Styling the Modal with CSS

Conclusion

This tutorial has been fairly lengthy, but it only scratches the surface of what’s possible with HTMX. As you’ve seen, HTMX makes dynamic updates in Django—like the modal we built—without much JavaScript.

I encourage you to explore the official documentation and try other components (dynamic forms, live search) to deepen your understanding.

The GitHub repository for this project is available here: https://github.com/DevWoody856/htmx_01 — Happy coding!