本文详解如何通过 html5 drag and drop api 与 flask 后端协同工作,实现在 kanban 看板中拖拽任务卡片并实时更新数据库状态,重点解决事件未触发、函数误渲染、子元素干扰等常见问题。
本文详解如何通过 html5 drag and drop api 与 flask 后端协同工作,实现在 kanban 看板中拖拽任务卡片并实时更新数据库状态,重点解决事件未触发、函数误渲染、子元素干扰等常见问题。
在 Flask 构建的 Kanban 应用中,仅靠 Jinja2 模板语法无法实现「拖拽后动态调用 Python 后端逻辑」——因为 {{ dropped(...) }} 是服务端渲染指令,会在页面生成时立即执行(传入的是模板变量而非运行时拖拽数据),而非在用户拖放动作发生时触发。真正的解决方案是:前端通过 Fetch API 主动发起异步请求,后端提供专用 POST 接口处理状态变更,并配合页面重载同步视图。
将原错误的内联模板调用替换为纯客户端 JavaScript 请求:
<script> const kanbanUrl = "{{ url_for('main.kanban', project_id=project.id) }}"; function allowDrop(ev) { ev.preventDefault(); } function dragEnter(ev, el) { ev.preventDefault(); ev.stopPropagation(); // 使用计数器避免子元素触发导致的闪烁 window.dragCounter = (window.dragCounter || 0) + 1; el.style.opacity = "1.0"; el.style.transform = "scale(1.03)"; } function dragLeave(ev, el) { ev.preventDefault(); ev.stopPropagation(); window.dragCounter = (window.dragCounter || 0) - 1; if (window.dragCounter === 0) { el.style.opacity = "0.7"; el.style.transform = "scale(1)"; } } function dragStart(ev) { ev.dataTransfer.setData("task_id", ev.target.id); } async function drop(ev, el) { ev.preventDefault(); ev.stopPropagation(); const taskId = ev.dataTransfer.getData("task_id"); const targetState = el.id; // 如 "todo", "inprogress", "done" try { const response = await fetch(kanbanUrl, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ task_id: taskId, target_state: targetState }) }); if (response.ok) { // 成功后强制刷新页面以反映数据库变更 location.reload(); } else { console.error("Drag-drop update failed:", await response.text()); } } catch (err) { console.error("Network error:", err); } }</script>
⚠️ 注意:ondrop="drop(event, this)" 中的 this 显式传递当前 <ol> 元素,确保 el.id 获取准确目标列 ID;同时所有事件处理器均需调用 ev.stopPropagation() 防止事件冒泡干扰。
在 kanban 路由中扩展对 JSON 请求的支持,避免与表单提交逻辑冲突:
from flask import request, jsonify, redirect, url_forfrom werkzeug.exceptions import [email protected]("/<int:project_id>/kanban", methods=["GET", "POST"])def kanban(project_id): project = Project.query.get(project_id) if not project: raise NotFound() try: session = sessions_by_project[project_id] except KeyError: raise NotFound() # 构建任务分组(按 status) tasks_by_status = defaultdict(list) for depth, task in walk_list(session.query(Task).all()): tasks_by_status[task.status.value].append((depth, task)) # 处理拖拽状态更新(JSON POST) if request.method == "POST" and request.is_json: data = request.get_json() if "task_id" in data and "target_state" in data: task_id = int(data["task_id"]) target_state = data["target_state"] task = session.query(Task).get(task_id) if not task: raise NotFound(f"Task {task_id} not found") task.change_status(str2status(target_state)) session.commit() return jsonify({"success": True, "message": "Status updated"}) # 处理传统表单提交(如点击任务按钮) if request.method == "POST" and "task" in request.form: return redirect(url_for("main.task", project_id=project_id, id=int(request.form["task"]))) return render_template("kanban.html", tasks_by_status=tasks_by_status, project=project)
确保 <ol> 容器正确声明事件监听器,并排除子元素干扰:
<ol class="kanban To-do" id="todo" ondrop="drop(event, this)" ondragover="allowDrop(event)" ondragenter="dragEnter(event, this)" ondragleave="dragLeave(event, this)"> <h2>To-Do</h2> {% for (depth, pending) in tasks_by_status[1] %} <li class="dd-item indent{{ depth }}" id="{{ pending.id }}" draggable="true" ondragstart="dragStart(event)" ondragend="dragEnd(event)"> <h3 class="title dd-handle"> <button name="task" value="{{ pending.id }}">{{ pending.title }}</button> </h3> <div class="text" contenteditable="true">{{ pending.description }}</div> </li> {% endfor %}</ol><!-- 同理定义 "inprogress" 和 "done" 列 -->
通过以上改造,即可实现平滑、可靠、符合 Web 标准的 Kanban 拖拽状态更新功能,兼顾代码可维护性与用户体验。