Skip to main content

Writing the code

Right now, when you tick a todo item, it gets crossed out. But it stays in the same spot in the list.

Goal

  • The home page shows unfinished todo items first, and done items at the bottom.
  • When the user clicks a checkbox, the server sends back the whole re-rendered list, in the right order.
  • HTMX swaps the new list 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 3-re-order-list branch.

1. Sort the list on the home page

Open todo/views.py. At the moment, the home view gets the todo items with TodoItem.objects.all(), which doesn’t promise any particular order. Change that line so the items are sorted:

todo/views.py
    todos = TodoItem.objects.order_by("done", "id")
    return render(request, "todo/home.html", {"todos": todos, "form": form})

order_by tells the database how to sort the results:

  • "done" sorts by the done field first. False comes before True, so unfinished items come first.
  • "id" breaks ties. Items with the same done value stay in the order they were added.

Refresh http://127.0.0.1:8000/. Any done items are now at the bottom. 🎉

Now tick an item. It gets crossed out, but it doesn’t move. Refresh the page and then it jumps to the bottom. That’s because the server only sorts the list when it renders the whole page. The toggle view still sends back a single item.

2. Move the list into its own partial

In goal 2, the toggle view rendered the todo item’s partial. This time, we want it to render the whole list. So the list needs its own partial too.

Create a new file, todo/templates/todo/partials/todo_list.html:

todo/templates/todo/partials/todo_list.html
<div class="space-y-3 flex-1 overflow-y-auto pr-2" id="todo-list">
    {% for item in todos %}
        {% include "todo/partials/todo_item.html" %}
    {% endfor %}
</div>

This is the list <div> from home.html, moved into its own file. We’ve added one thing: id="todo-list". We’ll use that to point HTMX at the list.

Now open todo/templates/todo/home.html. Replace the whole list <div> with an include of the new partial:

todo/templates/todo/home.html
{% extends "todo/_base.html" %}
{% block body %}
    <h1>Get this done</h1>
    {% include "todo/partials/todo_list.html" %}
    <h2 class="pt-5">Add new item</h2>
    ...

Refresh the page. It should look exactly the same as before. We’ve just moved some HTML around.

3. Send back the whole list

Open todo/views.py again. Change the last line of toggle_todo_done so it renders the list partial instead of the item partial:

todo/views.py
def toggle_todo_done(request, pk):
    todo = get_object_or_404(TodoItem, pk=pk)
    todo.done = not todo.done
    todo.save()
    todos = TodoItem.objects.order_by("done", "id")
    return render(request, "todo/partials/todo_list.html", {"todos": todos})
  • TodoItem.objects.order_by("done", "id") gets every todo item, sorted the same way as the home page.
  • The list partial expects a variable called todos, so that’s the name we pass it in.

Now try it. Refresh the page and tick an item.

It doesn’t work! The todo item you clicked turns into a whole copy of the list. Click a few more and the page fills up with lists inside lists.

Open your browser’s developer tools and look at the Elements tab (Inspector in Firefox). You’ll find a <div id="todo-list"> sitting where your todo item used to be.

That’s because we haven’t changed the checkbox. It still says hx-target="#todo-item-{{ item.id }}". So HTMX takes the whole list that the server sent back, and swaps it in place of the one todo item. We’ll fix that next.

4. Point HTMX at the list

Open todo/templates/todo/partials/todo_item.html. Change hx-target on the checkbox so it points at the list:

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-list"
       hx-swap="outerHTML">
  • hx-target="#todo-list" points at the list <div> from step 2.
  • hx-swap="outerHTML" is the same as before. The response is a whole list <div>, so it replaces the old list <div>.

The id on the todo item’s <div> isn’t used by HTMX any more. You can leave it there. It does no harm.

5. Try it out

Make sure the server is running, refresh http://127.0.0.1:8000/ and tick a todo item.

It gets crossed out and drops to the bottom of the list. 🎉 Untick it, and it moves back up with the other unfinished items.

Open the Network tab in your browser’s developer tools. Click on the toggle request and look at its Response. This time you’ll see the HTML for the whole list.

What we just did

In goal 2, the toggle request swapped one todo item. Now it swaps the whole list:

Two rows. The top row, labelled Before: swap one todo item, shows a Browser box holding a Page, with a Todo list box inside it holding two Todo item boxes. The first Todo item is labelled #todo-item-1. An arrow labelled request goes from the Page to a Django server box that renders one item, and an arrow labelled the todo item's HTML comes back to that one Todo item only. The bottom row, labelled After: swap the whole list, shows a Browser box holding a Page, with a Todo list box inside it labelled #todo-list, holding two Todo item boxes. An arrow labelled request goes from the Page to a Django server box that renders the whole list, and an arrow labelled the whole list's HTML comes back to the Todo list.
Figure 1: Before, the server sent back one todo item and HTMX swapped that item. Now it sends back the whole list, and HTMX swaps the whole list.
  1. You click a checkbox, and HTMX sends a POST request to toggle_todo_done.
  2. The view saves the change, gets every todo item in order, and renders todo/partials/todo_list.html.
  3. HTMX finds #todo-list on the page, because that’s what hx-target points at.
  4. hx-swap="outerHTML" replaces the old list <div> with the new one, so every item lands in its new spot.

Recap

  • We sorted the todo items with order_by("done", "id"), so unfinished items come first.
  • We moved the list into its own partial and gave it an id.
  • The toggle view now renders the whole list, because one change can move every item.
  • We pointed hx-target at the list, so HTMX swaps the whole list into the page.