我收到一个名为“TemplateSyntaxError”的错误,我创建了一个处理此应用程序中的任务的项目我创建了处理用户任务的task-board.html页面我为其创建了3个部分第1个部分添加了新任务第二个正在进行的任务,并且第三个已完成的任务,如果我想开始该任务,请将新任务部分移动到进行中部分,并将进行中的相同任务拖放到已完成部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task Board</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<style>
.task-section {
border: 1px solid #ccc;
margin: 10px;
padding: 10px;
width: 200px;
float: left;
}
</style>
</head>
<body>
<div class="task-section" id="new-section">
<h3>New</h3>
<div class="draggable" id="new">
{% for task in tasks.filter(status='new') %}
<div class="task" id="task_{{ task.id }}">{{ task.title }}</div>
{% endfor %}
</div>
</div>
<div class="task-section" id="in-progress-section">
<h3>In Progress</h3>
<div class="draggable" id="in_progress">
{% for task in tasks.filter(status='in_progress') %}
<div class="task" id="task_{{ task.id }}">{{ task.title }}</div>
{% endfor %}
</div>
</div>
<div class="task-section" id="completed-section">
<h3>Completed</h3>
<div class="draggable" id="completed">
{% for task in tasks.filter(status='completed') %}
<div class="task" id="task_{{ task.id }}">{{ task.title }}</div>
{% endfor %}
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(function() {
$(".draggable").sortable({
connectWith: ".draggable",
update: function(event, ui) {
var taskId = ui.item.attr("id").split("_")[1];
var newStatus = ui.item.parent().attr("id").split("-")[0];
$.ajax({
url: "/update_status/" + taskId + "/" + newStatus + "/",
type: "POST",
dataType: "json",
success: function(response) {
// Handle success if needed
},
error: function(error) {
// Handle error if needed
}
});
}
});
$(".draggable").disableSelection();
});
</script>
</body>
</html>
# Views.py
from django.shortcuts import render
from .models import Task
def task_board(request):
tasks = Task.objects.all()
return render(request, 'taskapp/task_board.html', {'tasks': tasks})
# models.py
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
status = models.CharField(max_length=20, choices=[
('new', 'New'),
('in_progress', 'In Progress'),
('completed', 'Completed'),
], default='new')
def __str__(self):
return self.title
# taskapp/urls.py
from django.urls import path
from .views import task_board
urlpatterns = [
path('taskapp-board/', task_board, name='task_board'),
]
# todo_project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('accounts.urls')),
path('taskapp/', include('taskapp.urls')),
]
TemplateSyntaxError at /taskapp/taskapp-board/
Could not parse the remainder: '(status='new')' from 'tasks.filter(status='new')'
Django 模板语言不支持调用需要参数的函数。您从
.filter(...)
方法中收到错误。您需要在视图中完成所有过滤,而不是在模板中。