Creating a modal window with Django, HTMX and hyperscript

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 begin with choosing a front-end framework. Developers accustomed to these frameworks might find Django somewhat old-fashioned. However, for those of us familiar with Django from 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 have wondered if a front-end framework is truly necessary just to build a single-page application (SPA). After all, combining Django's templating engine with CSS, vanilla JavaScript, and AJAX can get the job done. Sure, the code might not be as polished and structured as with a modern front-end framework. But with a few more elements to enhance its sophistication, Django could achieve the same goals—comfortably and with well-organized code. This is where HTMX comes in.

HTMX is gaining popularity among Django developers as a tool for creating SPA-like experiences without embracing a full JavaScript framework. Essentially, HTMX acts as a drop-in replacement for AJAX, allowing you to build dynamic and interactive interfaces while staying within 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 level up 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 create dynamic events, specifically 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

After that, the modal is removed after the CSS animation is completed.

Writing this in JavaScript would be quite a painstaking task, but surprisingly, hyperscript can be written quickly as if it were handling natural language.

10-2. htmx

10-3. Creating View

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

That's it! We have now implemented the same functionality as shown in the GIF animation at the beginning of this article.

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 simplifies creating dynamic updates in Django projects, like the modal window we built, without requiring extensive JavaScript. It's a powerful tool that can significantly streamline your front-end development workflow.

I encourage you to explore HTMX further. The official documentation is a great resource, and you could try building other interactive components, like dynamic forms or live search, to solidify your understanding.

The GitHub repository for this project is available here. Happy coding!