python - Delete() View 和模板代码

标签 python django

模板:

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

网址.py

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

这不起作用,请更正代码。

最佳答案

您的 URLConf 没有捕获任何数据来传递给变量 name。您需要将其作为 URL 的一部分进行捕获,或者将其保留在 POSTed 参数中进行捕获。

作为 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()

作为后置变量:

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

关于python - Delete() View 和模板代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2361074/

相关文章:

python播放列表解决方案,如何完成is_repeating_playlist函数?

python - %matplotlib qt 不工作

python - 使用 gspread 将 CSV 上传到 Google 表格

Django-rest-auth 使用 cookie 而不是 Authorization header

javascript - 来自 Python json.dumps 的 JSON 响应

python - Django:查询集对象过滤器,针对另一个对象的时间范围

python - drf-yasg/drf-spectaulous - 滤波器参数描述

python - 使用谷歌云函数生成Python脚本

python - matplotlib 中可能有奥利奥彩色文本吗?

Django 嵌套 if 语句给我一个奇怪的错误