没有确认模板的Django DeleteView [英] Django DeleteView without confirmation template
问题描述
我在模板中使用Django DeleteView,并创建了网址&视图。
是否可以跳过加载_confirm_delete模板并立即发布删除操作的过程。
I am using Django DeleteView in a template and I've created a url & view. Is it possible to skip the process of loading the _confirm_delete template and just post the delete immediately.
推荐答案
DeleteView
响应 POST
和 GET
请求, GET
请求显示确认模板,而 POST
删除实例。
DeleteView
responds to POST
and GET
requests, GET
request display confirmation template, while POST
deletes instance.
您可以发送 POST
请求,未经确认,格式如下:
You can send POST
request, without confirmation with form like this:
<form method="POST" action="{% url "your_delete_url_name" %}">
{% csrf_token %}<input type="submit" value="DELETE">
</form>
如果您不想使用链接代替表单按钮,请使用一些JavaScript制作不可见的表单,
If you do not want to have a link instead form button, use some javascript to make invisible form, that will be submitted on link click.
使用 GET
请求进行更新或删除不是一个好习惯,但是如果您真的坚持,可以在类视图中将 get
方法快捷方式
It is not good practice to use GET
request for updating or deleting, but if you really insist you can shortcut get
method in your class view to post
, ie:
def get(self, *args, **kwargs):
return self.post(*args, **kwargs)
这篇关于没有确认模板的Django DeleteView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!