HTMX Setup

Part 1, Chapter 4

You've already learned a little about HTMX in the introduction, but in this chapter we'll take a deeper dive.

What is HTMX?

So, the idea behind HTMX is "JavaScript for people who don't want to use JavaScript". When using HTMX, you can easily achieve dynamic page rendering just by using a handful of HTMX-specific attributes directly in your HTML.

The attributes you'll most often find yourself using are:

  1. hx-get/hx-post/hx-put/hx-patch/hx-delete
    • Core HTMX attributes.
    • Used to issue a GET/POST/PUT/PATCH/DELETE HTTP requests.
  2. hx-trigger
    • Defines which event will trigger the AJAX request (e.g., click, mouseenter).
  3. hx-target
    • Defines where the response will be loaded (e.g., #shopping-list).
  4. hx-swap
    • Defines how the content will be added to the target (e.g., beforeend appends the content after the last child in the target element).

Let's take a look at a straightforward example:

This is a simple webpage that, upon clicking the button, prints out a random cat fact. Try it out.

As you can see, there's no code in the JS tab -- everything is achieved in the HTML tab. There's a lot of code due to basic HTML requirements, but the part we're actually interested in is the button:

<button hx-get=""


  1. Clicking on the button will execute a GET request to the API (hx-get="").
  2. The response will replace the HTML inside (hx-swap="innerHTML") the element with an ID of catfact (hx-target="#catfact").

Simply put, the content of the <div id="catfact"></div> element will be the content of the response.

Tip: innerHTML is the default value of the hx-swap; you don't necessarily need to define it.


Inside the party app, create a "static/party/js" directory:

(venv)$ cd party && mkdir -p static/party/js

Move to the "party/static/party/js" directory and download the minified HTMX:

(venv)$ cd static/party/js && curl[email protected]/dist/htmx.min.js --output htmx.min.js

Modern JavaScript frameworks mostly advise against using public CDNs. See the HTMX docs or the Tailwind CSS docs for more info.

This is the directory structure that you should have:

├── .gitignore
├── core
│   ├──
│   ├──
│   ├──
│   ├──
│   └──
├── party
│   ├──
│   ├──
│   ├──
│   ├── fixtures
│   │   ├── initial_gifts.json
│   │   ├── initial_guests.json
│   │   └── initial_parties.json
│   ├── migrations
│   │   ├──
│   │   ├──
│   │   └──
│   ├──
│   ├── tests
│   │   └──
│   ├── static
│   │   └── party
│   │       └── js
│   │           └── htmx.min.js
│   ├── templates
│   │   └── party
│   │       └── base.html
│   └──
├── pytest.ini
└── requirements.txt

Include HTMX into your base template:


{% load static %} <!--NEW-->
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <script type="text/javascript" src="{% static '/party/js/htmx.min.js' %}"></script> <!--NEW-->
    {% block content %}
    {% endblock %}

Commit and push the changes:

(venv)$ git add -A
(venv)$ git commit -m 'HTMX library added.'
(venv)$ git push -u origin main

What Have You Done?

You've added the HTMX library to your project. Since using HTMX with Django requires creation of multiple templates, URLs, and views, we won't play around with it in this chapter.

In the next chapter, we'll make our basic site prettier with Tailwind CSS.

Mark as Completed