如何解决找不到带有参数“'',”的“ customerdel”尝试了1个模式:['dashboard / records / customerdel /?P <pk> [0-9] +$']
我正在尝试使用简单的html按钮从数据库中删除对象。另外,尝试实现“您确定”消息吗?但是我每次都会收到此错误,而且我无法破解。
这是我的查看功能。
def customerdel(request,pk):
objs = Customer.objects.filter(id=pk)
if request.method == 'POST':
objs.delete()
# messages.success(request,'Successfully deleted')
return render(request,'records.html')
else:
content ={
'items':Customer.objects.all
}
return render(request,'delete.html',content)
这是record.html页面
<h1>Record's page</h1>
{% for abc in Customerdata %}
{{abc.name}}
{{abc.pk}}
<form >
<button class="btn btn-danger btn-sm">
<a href="{% url 'customerdel' abc.pk %}">Delete</a></button>
</form>
{% endfor %}
这是delete.html页面
<h1>Welcome to Delete page</h1>
<p>Are you sure want to del {{items.name}} ??</p>
<form action="{% url 'customerdel' items.pk %}" method="post">
{% csrf_token %}
<a href="{% url 'records' %}">Cancel</a>
<input name="confirm" type="submit" >
</form>
这是我的网址。
path('dashboard/records/customerdel/<int:pk>',views.customerdel,name='customerdel'),
解决方法
在渲染模板时您做错了,因为您没有向模板发送任何数据。但是在record.html内,您试图遍历未定义的CustomerData,因此将其发送到模板或另一种方法是只需重定向到用于呈现record.html的路由即可。
这是您应该在django项目中应用的完整代码,以使其正常工作。
from django.http import HttpresponseRedirect
from django.urls import reverse #these two lines are required for redirecting to a previous route(url)
因为您必须设置一个将记录功能带到这样的网址;
path('records',views.records,name='records')
如果尚未添加名称,请添加它。
然后在customerdel函数的views.py中找到
;if request.method == 'POST':
#write your code which you already wrote and instead of return render(...) write this;
return HttpResponseRedirect(reverse('records')) #where records is the name of the url which takes us to the records page.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。