tags:

views:

73

answers:

2

Template:

<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> 

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.

A: 
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 }}/">
Amarghosh
@amarghosh In my template drop down box is not fetching data from db.Where is the bug?
DAFFODIL
@daffodil It depends on the original view - you've posted the view for delete button's click handler.
Amarghosh
+1  A: 

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()
jcdyer
I have problem with that url,which you have stated above Page not found (404)Request Method: POSTRequest URL: http://127.0.0.1:8000/customer/(?P%3Cname%3E[a-z]*)/delete/ .This is the error.
DAFFODIL
There was a missing parenthesis. It's fixed now.
jcdyer
My self corrected it,but still it is the same.Thnx for your help.Plz see,this you will know where is the mistk http://dpaste.com/hold/167216/
DAFFODIL
delete view,it is stating this error delete() takes exactly 2 arguments (1 given)
DAFFODIL