我是Django的新面孔,所以如果我的问题是愚蠢的,请考虑一下。在这种情况下,我想删除数据库中的一个项目,如果我点击按钮,html将传递一个id给views.py
,然后函数Delete()
将删除这个项目。但是无论我尝试什么,它都没有任何响应。
现在我在home.html
中添加action="{% url 'todolist:delete' todo.id %}"
,但它遇到了一些错误:NoReverseMatch: Reverse for 'delete' with arguments '('',)' not found. 1 pattern(s) tried: ['$']
,我不知道怎么解,请给予我一些建议,我会很感激的。
我的代码如下:
项目/urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('todolist.urls', namespace='todolist')),
]
todolist/urls.py:
app_name = "todolist"
urlpatterns = [
path('', views.Add, name = 'add'),
path('', views.Delete, name = 'delete'),
]
home.html:
<form action="{% url 'todolist:delete' todo.id %}" method="post">
{% csrf_token %}
<table>
<tbody>
{% for each in todo_list %}
<tr>
<td><div class="thing"><input type="checkbox" name="done" value="done"><label>{{each.todo}}</label></div></td>
<td><input type="button" class="edit" name="edit" value="E"></td>
<td><input type="button" class="delete" name="delete" value="x"></td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
views.py:
def Delete(request, id):
if 'delete' in request.POST:
todo = Todo.objects.get(id=id)
todo.delete()
todo_list = Todo.objects.all()
return render(request, 'todolist/home.html', {'todo_list': todo_list, 'todo': todo})
2条答案
按热度按时间j8yoct9x1#
你的URL看起来不太好。如果你看一下下面的代码:
但是,错误不是来自于此。它是在谈论论点。所以:
现在,您的
delete
路径将从以下位置获取参数:在这里你可以看到更多关于路径。
toiithl62#
Django模板方法(FormClass,TableClass)
(urls.py)
(Views.py)
我已经在DeleteLabelView中将action.html作为template_name文件传递,而不是需要传递template_name = '/delete_label.html'
这就是为什么我得到这个错误。