开发者

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 }}/">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜