模板:
<form method="POST" action="/customer/delete/">
<div style="float: right;
margin: 0px; padding: 05px; ">
Name:<select name="customer">
{% for customer in customer %}
<option value="{{ customer.name|escape }}" ></option><br />
{% endfor %}
</select>
<input type=submit value="delete">
</div>
</form>
查看:
def delete(request, name):
Customer.objects.get(name=name).delete()
return HttpResponse('deleted')
Urls.py
(r'^customer/delete/', 'quote.excel.views.delete'),
这不起作用,请更正代码。
答案 0 :(得分:1)
你的URLConf没有捕获任何数据传递给变量name
。您需要将其作为URL的一部分捕获,或者将其留在POSTed参数中。
作为网址的一部分:
(r'^customer/(?P<name>[a-z]*)/delete/', 'quote.excel.views.delete')
def delete(request, name):
if request.method == "POST":
# GET requests should NEVER delete anything,
# or the google bot will wreck your data.
Customer.objects.get(name=name).delete()
作为职位变量:
(r'^customer/delete/', 'quote.excel.views.delete')
def delete(request): # No arguments passed in
if request.method == "POST":
name = request.POST['name']
Customer.objects.get(name=name).delete()
答案 1 :(得分:0)
Customer.objects.get(name = request.POST['name']).delete()
顺便说一下,您确定模板中的action
变量确实是'delete'
吗?如果不是,则调用的url(因此方法)将不同。
<form method="POST" action="/customer/{{ action }}/">