找不到 'delete_task/{{todo.id}}' 的反向。 'delete_task/{{todo.id}}' 不是有效的视图函数或模式名称

Reverse for 'delete_task/{{todo.id}}' not found. 'delete_task/{{todo.id}}' is not a valid view function or pattern name

我是 django 的新手,在 运行 之后我遇到了以下问题: 找不到 'delete_task/{{todo.id}}' 的反转。 'delete_task/{{todo.id}}' 不是有效的视图函数或模式名称。

在我的模板文件中 index.html 我有:

<form action="{% url 'delete_task/todo.id'%}" method="post" class = 'delete-link'>
    {% csrf_token%}
    <button type="submit">Delete</button>
</form>

我的urls.py:

urlpatterns = [
path('delete_task/<int:todo_id>', views.delete_task, name='delete_task'),

]

我的views.py:

def delete_task(request, task_id):
    return HttpResponse(task_id)
#the delete_task block is just for test

谢谢

这样写:

<form action="{% url 'delete_task' todo.id %}" method="post" class = 'delete-link'>
{% csrf_token%}
<button type="submit">Delete</button>

问题解决

路径的名称delete_task,所以{% url … %} template tag [Django-doc]的第一个参数是'delete_task,那么第二个就是参数(此处todo.pk):

<form action="<b>{% url 'delete_task' todo.id %}</b>" method="post" class = 'delete-link'>
    {% csrf_token%}
    <button type="submit">Delete</button>
</form>