【发布时间】:2022-07-25 17:59:23
【问题描述】:
我有这样的表格:
<form method=\"post\">
{% csrf_token %}
<div class=\"mb-3\">
<input type=\"hidden\" class=\"form-control\" id=\"url\" name=\"delete-id\" value=\"{{ url.id }}\">
</div>
<div class=\"mb-3\">
<button type=\"submit\" class=\"btn btn-primary mb-3\">Delete</button>
</div>
</form>
此表单嵌入在表格的单元格中。 我在表单中提出了一个发布请求,但是当我测试 request.method 时,它总是给我 GET 答案。
if request.method == \'post\':
Url.objects.get(id=request.POST.get[\'delete-id\']).delete()
这里 request.method 总是 GET ,我不明白为什么。
这是我的整个功能:
@login_required
def website(request, website_id):
if request.method == \'POST\':
Url.objects.get(id=request.GET[\'delete-id\']).delete()
customer_id = request.session.get(\'customer_id\')
context = {
\'website\': Website.objects.get(id=website_id, customer__id=customer_id)
}
return render(request, \'main/website.html\', context)
-
即使在提交表单后,您是否将 request.method 作为 GET ?
-
是的,即使按下删除按钮,我也会得到 GET
-
如果你得到它为
GET,那么你有问题<form method=\"post\">- 也许你只有<form>而没有method- 在网络浏览器中使用Ctr+U来查看页面的源代码。也许您使用错误的模板生成页面。 -
如果可以从您的视图和模板文件中添加更多内容
-
页面源未更新。我在 docker 容器上运行。方法是在页面源中删除。这就是为什么它不起作用。
标签: python django html-table csrf delete-row