2

我是 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'),

]

我的意见.py:

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

谢谢

4

2 回答 2

3

路径的名字delete_task,所以{% url … %}模板标签[Django-doc]的第一个参数是'delete_task,那么第二个就是参数(这里是todo.pk):

<form action="{% url 'delete_task' todo.id %}" method="post" class = 'delete-link'>
    {% csrf_token%}
    <button type="submit">Delete</button>
</form>
于 2020-07-09T18:27:07.880 回答
2

像这样写:

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

问题解决

于 2020-07-09T18:26:55.077 回答