首页 > 后端开发 > Python教程 > 使用 Django 和 HTMX 创建待办事项应用程序 - 创建前端并添加 HTMX 部分

使用 Django 和 HTMX 创建待办事项应用程序 - 创建前端并添加 HTMX 部分

Susan Sarandon
发布: 2025-01-06 00:00:40
原创
224 人浏览过

欢迎来到我们系列的第三部分!在本系列文章中,我记录了自己对 HTMX 的学习,使用 Django 作为后端。
如果您刚刚接触该系列,您可能需要先查看第一部分和第二部分。

创建模板和视图

我们将首先创建一个基本模板和一个指向索引视图的索引模板,该索引视图将列出数据库中的待办事项。我们将使用 DaisyUI(Tailwind CSS 的扩展)来使 Todos 看起来更漂亮。

这是设置视图后、添加 HTMX 之前页面的样子:

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

添加视图和 URL

首先,我们需要更新项目根目录中的 urls.py 文件,以包含我们将在“核心”应用程序中定义的 url:

# 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
]
登录后复制

然后,我们为应用程序定义新的 URL,添加新文件 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"),
]
登录后复制

现在我们可以创建相应的视图了,在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)

登录后复制

这里有一些有趣的事情:我们的索引路由(主页)将仅重定向到任务 URL 和视图。这将使我们能够在未来自由地为应用程序实现某种登陆页面。

任务视图需要登录,并在上下文中返回两个属性:用户的全名(如果需要的话会合并到用户名)和待办事项,按创建日期排序(我们可以在未来)。

现在让我们添加模板。我们将为整个应用程序提供一个基本模板,其中包括 Tailwind CSS 和 DaisyUI,以及任务视图的模板。

<!-- 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>



<p>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.</p>

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

<pre class="brush:php;toolbar:false"><!-- core/templates/tasks.html -->

{% extends "_base.html" %}

{% block content %}
<div>



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

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

<h2>
  
  
  Add inline partial templates
</h2>

<p>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 <strong>server-driven</strong> updates, leveraging <strong>hypermedia</strong> (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.</p>

<p>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.</p>

<p>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.<br>
</p>

<pre class="brush:php;toolbar:false">❯ uv add django-template-partials
Resolved 24 packages in 435ms
Installed 1 package in 10ms
 + django-template-partials==24.4
登录后复制

按照安装说明,我们应该更新我们的settings.py 文件

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "core",
    "template_partials",  # <-- NEW
]
登录后复制

在我们的任务模板中,我们将每个待办事项定义为内联部分模板。如果我们重新加载页面,它不应该有任何视觉差异。

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

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

{% block content %}
<div>



<p>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.</p>

<p>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.</p>

<h2>
  
  
  Toggling todo items on and off with HTMX
</h2>

<p>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.</p>

<p>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:<br>
</p>

<pre class="brush:php;toolbar:false">    <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 %}

登录后复制

在 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"),
    path("tasks/<int:task_id>/", views.toggle_todo, name="toggle_todo"), # <-- NEW
]
登录后复制

然后,在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
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})

登录后复制

在 return 语句中,我们可以看到如何利用模板部分:通过引用其名称 todo-item-partial 以及与我们要处理的项目名称相匹配的上下文,我们仅返回部分在tasks.html中的循环中进行迭代。

我们现在可以测试打开和关闭该项目:

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

看起来我们只是在做一些客户端工作,但是检查浏览器中的网络工具向我们展示了如何分派 PUT 请求并返回部分 HTML:

PUT 请求

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

回复

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

我们的应用程序现已 HTMX 化!您可以在此处查看最终代码。在第 4 部分中,我们将添加添加和删除任务的功能。

以上是使用 Django 和 HTMX 创建待办事项应用程序 - 创建前端并添加 HTMX 部分的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板