Please delete me
Please delete me

Reputation:

Django edit and delete from front end

Views.py

def delete(request):
    customer = Customer.objects.get(id=5)
    customer.delete()
    return HttpResponse('deleted')

Template.py

<form method="POST" action="/customer/">
 <div style="float: right; 
              margin: 0px; padding: 05px; ">
   <p> Name : <select name ="delete_user">
   {% for customer in customer %}
   <option value = "{{ customer }}"> {{ customer.name }} </option>
  {% endfor %}
</select>
</p>
<p><input type="submit" value="delete"></p>
 </div><br />

urls.py

(r'^delete/$','quote.excel.views.delete'), 

This isnot working,it needs to ftch users from database ,in drop down box ,when i select a particular user and click delete,it should delete the name.

This is the error,i m getting

DoesNotExist at /delete/

Customer matching query does not exist.

Upvotes: 0

Views: 2445

Answers (2)

luc
luc

Reputation: 43096

all returns all the customers. use get to get only 1 (by id or name)

You need to get the user id. Put that info in your form and handle in the deletion view

Something like

data = request.POST.copy()
c_id = int(data.get('id', '0'))
if c_id>0:
    customer = Customer.objects.get(id=c_id)
    customer.delete()

I recommend to look at teh Django tutorial http://docs.djangoproject.com/en/1.1/intro/tutorial04/ .That's explain very well how to use forms in Django

Upvotes: 3

Desintegr
Desintegr

Reputation: 7090

Django is a very well documented project. Please take a look at it.

Also refer to the Model API and deleting objects documentations.

Upvotes: 0

Related Questions