DEV Community

rodbv
rodbv

Posted on

Creating a To-Do app with Django and HTMX - Part 3: Creating the frontend and adding HTMX

Welcome to part 3 of our series! In this series of articles, I am documenting my own learning of HTMX, using Django for the backend.
If you just arrived in the series you may want to check parts one and two first.

Creating the templates and views

We will start by creating a base template, and an index template that points to an index view, that will list the Todos we have in the database. We will use DaisyUI which is an extension of Tailwind CSS, to make the Todos decent-looking.

This is how the page to look like once the views are set, and before we add HTMX:

To-do page, with a blue background and a list with 4 to-do items with checkbox to indicate completion and title, in a DaiyUI list component

Adding the views and URLs

First we need to update the urls.py file in the root of the project, to include the urls that we will define in our "core" app:

# todomx/urls.py

from django.contrib import admin
from django.urls import include, path # <-- NEW

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("core.urls")), # <-- NEW
]
Enter fullscreen mode Exit fullscreen mode

Then, we define the new URLs for the app, placing adding new file core/urls.py:

# core/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("tasks/", views.tasks, name="tasks"),
]
Enter fullscreen mode Exit fullscreen mode

Now we can create the corresponding views, in core/views.py

# core/views.py

from django.shortcuts import redirect, render
from .models import UserProfile, Todo
from django.contrib.auth.decorators import login_required


def index(request):
    return redirect("tasks/")


def get_user_todos(user: UserProfile) -> list[Todo]:
    return user.todos.all().order_by("created_at")


@login_required
def tasks(request):
    context = {
        "todos": get_user_todos(request.user),
        "fullname": request.user.get_full_name() or request.user.username,
    }

    return render(request, "tasks.html", context)

Enter fullscreen mode Exit fullscreen mode

A few interesting things here: our index route (home page) will just redirect to the tasks URL and view. This will give us the freedom to implement some sort of landing page for the app in the future.

The tasks view requires login, and returns two attributes in the context: the user's fullname, which coalesce to their username if needed, and the todo items, sorted by creation date (we can add some sorting options for the user in the future).

Now let's add the templates. We'll have a base template for the whole app, which will include Tailwind CSS and DaisyUI, and the template for the tasks view.

<!-- core/templates/_base.html -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title></title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link href="https://cdn.jsdelivr.net/npm/daisyui@5.0.0-beta.1/daisyui.css" rel="stylesheet" type="text/css"/>
    <script src="https://cdn.tailwindcss.com?plugins=typography"></script>
    {% block header %}
    {% endblock %}
  </head>
  <body class="h-screen bg-gradient-to-b from-blue-50 to-blue-300">
    {% block content %}
    {% endblock %}
  </body>
</html>
{% block js %}
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

Note that we're adding Tailwind and DaisyUI from a CDN, to keep these articles simpler. For production-quality code, they should be bundled in your app.

We're using the beta version of DaisyUI 5.0, which includes a new list component which suits our todo items fine.

<!-- core/templates/tasks.html -->

{% extends "_base.html" %}

{% block content %}
<div class="flex flex-col items-center mx-10 md:mx-20">
  <h1 class="text-2xl font-bold m-4">{{ fullname }}'s Tasks</h1>
  <div class="w-full max-w-2xl">
    <ul class="list bg-base-100 rounded-box shadow-md">
      {% for todo in todos %}
      <li class="list-row">
        <input type="checkbox" {% if todo.is_completed %}checked{% endif %} class="checkbox checkbox-lg checkbox-info mr-4" />
        <span class="flex-1 text-lg {% if todo.is_completed %}text-gray-500{% endif %}">{{ todo.title }}</span>
      </li>
      {% endfor %}
    </ul>
  </div>
</div>
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

We can now add some Todo items with the admin interface, and run the server, to see the Todos similarly to the previous screenshot.

We're now ready to add some HTMX to the app, to toggle the completion of the item

Add inline partial templates

In case you're new to HTMX, it's a JavaScript library that makes it easy to create dynamic web pages by replacing and updating parts of the page with fresh content from the server. Unlike client-side libraries like React, HTMX focuses on server-driven updates, leveraging hypermedia (HTML) to fetch and manipulate page content on the server, which is responsible for rendering the updated content, rather than relying on complex client-side rendering and rehydration, and saving us from the toil of serializing to and from JSON just to provide data to client-side libraries.

In short: when we toggle one of our todo items, we will get a new fragment of HTML from the server (the todo item) with its new state.

To help us achieve this we will first install a Django plugin called django-template-partials, which adds support to inline partials in our template, the same partials that we will later return for specific todo items.

❯ uv add django-template-partials
Resolved 24 packages in 435ms
Installed 1 package in 10ms
 + django-template-partials==24.4
Enter fullscreen mode Exit fullscreen mode

Following the installation instructions, we should update our settings.py file as such

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "core",
    "template_partials",  # <-- NEW
]
Enter fullscreen mode Exit fullscreen mode

In our tasks template, we will define each todo item as an inline partial template. If we reload the page, it shouldn't have any visual differences.

<!-- core/templates/tasks.html -->

{% extends "_base.html" %}
{% load partials %} <!-- NEW -->

{% block content %}
<div class="flex flex-col items-center mx-10 md:mx-20">
  <h1 class="text-2xl font-bold m-4">{{ fullname }}'s Tasks</h1>
  <div class="w-full max-w-2xl">
    <ul class="list bg-base-100 rounded-box shadow-md">
      {% for todo in todos %}
      {% partialdef todo-item-partial inline %} <!-- NEW --> 
      <li class="list-row">
        <input type="checkbox" {% if todo.is_completed %}checked{% endif %} class="checkbox checkbox-lg checkbox-info mr-4" />
        <span class="flex-1 text-lg {% if todo.is_completed %}text-gray-500{% endif %}">{{ todo.title }}</span>
      </li>
      {% endpartialdef %}  <!-- NEW --> 
      {% endfor %}
    </ul>
  </div>
</div>
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

The two attributes added are important: the name of the partial, todo-item-partial, will be used to refer to it in our view and other templates, and the inline attribute indicates that we want to keep rendering the partial within the context of its parent template.

With inline partials, you can see the template within the context it lives in, making it easier to understand and maintain your codebase by preserving locality of behavior, when compared to including separate template files.

Toggling todo items on and off with HTMX

To mark items as complete and incomplete, we will implement a new URL and View for todo items, using the PUT method. The view will return the updated todo item rendered within a partial template.

First of all we need to add HTMX to our base template. Again, we're adding straight from a CDN for the sake of simplicity, but for real production apps you should serve them from the application itself, or as part of a bundle. Let's add it in the HEAD section of _base.html, right after Tailwind:

    <link href="https://cdn.jsdelivr.net/npm/daisyui@5.0.0-beta.1/daisyui.css" rel="stylesheet" type="text/css"/>
    <script src="https://cdn.tailwindcss.com?plugins=typography"></script>
    <script src="https://unpkg.com/htmx.org@2.0.4" ></script> <!-- NEW -->
    {% block header %}
    {% endblock %}

Enter fullscreen mode Exit fullscreen mode

On core/urls.py we will add our new route:

# core/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("tasks/", views.tasks, name="tasks"),
    path("tasks/<int:task_id>/", views.toggle_todo, name="toggle_todo"), # <-- NEW
]
Enter fullscreen mode Exit fullscreen mode

Then, on core/views.py, we will add the corresponding view:

# core/views.py

from django.shortcuts import redirect, render
from .models import UserProfile, Todo
from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_http_methods # <-- NEW

# ... existing code

# NEW
@login_required
@require_http_methods(["PUT"])
def toggle_todo(request, task_id):
    todo = request.user.todos.get(id=task_id)
    todo.is_completed = not todo.is_completed
    todo.save()

    return render(request, "tasks.html#todo-item-partial", {"todo": todo})

Enter fullscreen mode Exit fullscreen mode

In the return statement, we can see how we can leverage template partials: we're returning only the partial, by referring to its name todo-item-partial, and the context that matches the name of the item we're iterating in the loop in tasks.html.

We can now test toggling the item on and off:

GIF showing todo items being toggled on and off

It looks like we're just doing some client-side work, but inspecting the Network tool in the browser shows us how we're dispatching PUT requests and returning the partial HTML:

PUT request

Image of the PUT request to /tasks/2

Response

Response of the PUT request with the HTML partial

Our app is now HTMX-fied! You can check the final code here. In part 4, we will add the ability to add tasks.

Top comments (0)