Delete() views and template code
Template:
<form method="POST" action="/customer/delete/">
<div style="float: right;
margin开发者_如何学C: 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>
Views:
def delete(request, name):
Customer.objects.get(name=name).delete()
return HttpResponse('deleted')
Urls.py
(r'^customer/delete/', 'quote.excel.views.delete'),
This isnt working,plz correct the code.
Your URLConf isn't catching any data to pass on to the variable name
. You need to either catch it as a part of the URL, or leave it to catch in a POSTed argument.
As part of the URL:
(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()
As a post variable:
(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()
Customer.objects.get(name = request.POST['name']).delete()
By the way, are you sure that the action
variable in the template is indeed 'delete'
? If it isn't, the url called (and hence the method) will be different.
<form method="POST" action="/customer/{{ action }}/">
精彩评论