这是我的观点。
def delete_chat(request, id):
chat=get_object_or_404(Chat,id=id)
chat.delete()
return redirect('msgs:inbox')
这是我的模板。
<a href="{% url 'msgs:delete_chat' id=Chat.id %}" class="parag delete-btn">Delete chat</a>'+
这是我的错误。
NoReverseMatch at /messages/inbox/
Reverse for 'delete_chat' with keyword arguments '{'id': ''}' not found. 1 pattern(s) tried:
['messages\\/inbox\\/delete\\/$']
谁能帮我弄清楚该把id传给谁?
你的URL模式没有提供id。我们可以在错误信息中看到这一点。
['messages\\/inbox\\/delete\\/$']
.
这个url应该是。
urlpatterns += [
path('inbox/delete/<int:id>/', name='delete_chat')
]
另外你的 Chat.id
变量是空的,这意味着要么聊天变量不存在,要么它没有id属性,要么它的id属性是空的。keyword arguments '{'id': ''}'
这意味着要么聊天变量不存在 要么它没有id属性 要么它的id属性为空。我们不能确切的说是哪一种,因为你只显示了模板的一行。
正确的格式:a href="{% url 'msgs:delete_chat' chat.id %}"
https:/docs.djangoproject.comen3.0introutorial03#removing-hardcoded-urls-in-templates。
试着用小写的方式调用你的变量*。
您没有删除聊天视图的模板,所以页面没有任何地方可以重定向。正确的格式: href="{% url 'msgs:delete_chat' chat.id %}" 其中 chat 应该是视图中定义的帖子的上下文