Skip to main content

Writing the code

Right now, when you tick a todo item, it gets saved. But it doesn’t get crossed out until you refresh the page.

Goal

  • When the user clicks a checkbox, the server sends back the re-rendered todo item.
  • HTMX swaps the new todo item into the page, in place of the old one.
Tip

If you get stuck, you can see the finished code for this goal on the 2-check-box-re-render-crossed-out branch.

1. Send back the partial

Open todo/views.py. At the moment, toggle_todo_done sends back an empty response. Change the last line so it renders the todo item’s partial instead:

todo/views.py
def toggle_todo_done(request, pk):
    todo = get_object_or_404(TodoItem, pk=pk)
    todo.done = not todo.done
    todo.save()
    return render(request, "todo/partials/todo_item.html", {"item": todo})

This is the same partial the home page uses for each item in the list. The partial expects a variable called item, so that’s the name we pass it in.

2. Give each todo item an id

Open todo/templates/todo/partials/todo_item.html. We need a way to point at a specific todo item, so add an id to the outer <div>:

todo/templates/todo/partials/todo_item.html
<div class="bg-white border border-gray-200 rounded-lg shadow-sm p-4"
     id="todo-item-{{ item.id }}">
    ...
  • todo-item- is plain text. It ends up in the HTML exactly as written.
  • { item.id } is a Django template tag. When Django renders the partial, it swaps this out for the todo item’s id from the database.

So every todo item gets its own id, like todo-item-1, todo-item-2 and so on.

Go and see for yourself. Refresh http://127.0.0.1:8000/, open your browser’s developer tools and look at the Elements tab (it’s called Inspector in Firefox). Expand the todo items and you’ll see each outer <div> has its own id. You can also right-click a todo item and choose Inspect to jump straight to it.

Notice that there’s no { item.id } in there. The browser only ever sees the finished HTML that Django sends it.

An id is a name you give to one element on the page. Each id should only be used once per page, so it points at exactly one thing.

<div id="greeting">Hello!</div>

You can then find that element with a CSS selector, which is the id with a # in front: #greeting. HTMX uses these selectors to work out which element you mean.

3. Tell HTMX where to put the response

In the same file, add hx-target and hx-swap to the checkbox. It’s getting long, so we’ll split it over a few lines:

todo/templates/todo/partials/todo_item.html
<input type="checkbox"
       {% if item.done %}checked{% endif %}
       hx-trigger="click"
       hx-post="{% url 'toggle_todo_done' item.pk %}"
       hx-target="#todo-item-{{ item.id }}"
       hx-swap="outerHTML">
  • hx-target="#todo-item-{{ item.id }}" points at the todo item <div> that this checkbox lives in.
  • hx-swap="outerHTML" replaces that whole <div> with the response from the server.
WarningDon’t forget the #

A common mistake is leaving out the # in hx-target. The id on the <div> has no #, but hx-target needs one, because it takes a CSS selector.

  • ✅ hx-target="#todo-item-{{ item.id }}"
  • ❌ hx-target="todo-item-{{ item.id }}"

Without the #, HTMX can’t find the element, so nothing on the page changes.

Here’s the whole partial now:

todo/templates/todo/partials/todo_item.html
<div class="bg-white border border-gray-200 rounded-lg shadow-sm p-4"
     id="todo-item-{{ item.id }}">
    <div class="flex items-center gap-1 mb-1">
        <input type="checkbox"
               {% if item.done %}checked{% endif %}
               hx-trigger="click"
               hx-post="{% url 'toggle_todo_done' item.pk %}"
               hx-target="#todo-item-{{ item.id }}"
               hx-swap="outerHTML">
        <p class="font-bold mb-0 {% if item.done %}line-through text-gray-400{% endif %}">{{ item.title }}</p>
    </div>
    <p class="mb-0 {% if item.done %}line-through text-gray-400{% else %} text-gray-600 {% endif %}">{{ item.description }}</p>
</div>

4. Try it out

Make sure the server is running, open http://127.0.0.1:8000/ and click a checkbox.

The item gets crossed out straight away. 🎉 Untick it, and the line goes away again.

Open your browser’s developer tools and look at the Network tab. Click on the toggle request and look at its Response. You’ll see the HTML for just that one todo item.

Recap

  • The view now renders the todo item’s partial and sends back the HTML.
  • We gave each todo item a unique id.
  • We used hx-target to say where the response should go, and hx-swap="outerHTML" to replace the whole todo item.