You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django+HTMX动态加载页面后刷新内容丢失问题求助

问题原因

你遇到的核心问题是:点击「添加任务」时,HTMX将add_task.html片段插入到主页的#main容器,同时通过hx-push-url="true"修改了浏览器地址栏为/create_todo/。但刷新页面时,浏览器直接请求这个URL,而你的create_todo视图GET请求仅返回add_task.html片段,没有包含导航栏、页面框架等完整结构,导致显示异常。


解决方案

下面提供两种实用的解决方式,任选其一即可:

方式一:让create_todo路由返回完整页面

修改视图逻辑,当GET请求访问/create_todo/时,返回包含导航栏的完整页面,而非片段:

  1. 更新views.py的create_todo函数:
def create_todo(request):
    if request.method == "POST":
        # 原POST逻辑保持不变
        title = request.POST.get("title")
        description = request.POST.get("description")
        due_date = request.POST.get("due_date")
        todo = Todo.objects.create(title=title, description=description, due_date=due_date)
        
        todos = Todo.objects.all().order_by("-id")
        context = {"todos": todos}
        return render(request, "todo/todo-list.html", context)
    elif request.method == "GET":
        # 返回完整页面,而非片段
        return render(request, "todo/create_todo_full.html")
  1. 创建create_todo_full.html完整页面:
{% extends "../base.html" %}

{% block title %}Add Task{% endblock title %}

{% block content %}
    {% include "./navbar.html" %}
    <div class="px-20" id="main">
        {% include "./add_task.html" %}
    </div>
{% endblock content %}

这样刷新/create_todo/时,浏览器会加载包含导航栏的完整页面,和HTMX动态加载的效果完全一致。


方式二:在主页加载时自动匹配URL渲染内容

保持视图返回片段不变,修改主页逻辑,当用户直接访问/create_todo/时,自动用HTMX加载添加任务内容:

更新home.html的#main容器部分:

<div class="px-20" id="main" 
     {% if request.path == "/create_todo/" %}
         hx-get="{% url 'create_todo' %}" 
         hx-trigger="load"
     {% endif %}>
    {% if request.path != "/create_todo/" %}
        <!-- 原主页内容 -->
        <div class="flex justify-between">
            <h3 class="mt-5 text-center text-xl text-gray-500 font-bold my-8">Today's Tasks</h3>
            <div class="flex justify-center items-center">
                <button class="bg-blue-700 rounded text-white font-bold text-sm px-4 py-1">View Calendar</button>
            </div>
        </div>
        <div class="flex">
            <div class="flex justify-center items-center">
                <button class="bg-blue-700 rounded text-white font-bold text-sm px-4 py-1">All</button>
            </div>
            <div class="flex justify-center items-center ml-4">
                <button class="bg-blue-700 rounded text-white font-bold text-sm px-4 py-1">Completed</button>
            </div>
            <div class="flex justify-center items-center ml-4">
                <button class="bg-blue-700 rounded text-white font-bold text-sm px-4 py-1">Incomplete</button>
            </div>
        </div>

        <div id="all-tasks">
            {% include "./todo-list.html" %}
        </div>
        <div id="completed-tasks">
        </div>
        <div id="incomplete-tasks">
        </div>
    {% endif %}
</div>

此方案利用Django的request.path判断当前URL,若为/create_todo/,则在页面加载时自动触发HTMX请求加载添加任务片段,保持页面结构完整。


内容的提问来源于stack exchange,提问作者Talemwa Eria Jackson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 20:05:34