删除Django中的对象 [英] deleting an object in django

查看:73
本文介绍了删除Django中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中删除任务,所以我使用此代码
这是我的删除视图

i want to delete a task from the database so i use this code this is my delete view

def task_Delete(request,id=None):
        if request.method == 'POST':
            form = TaskForm()
            id = int(request.POST.get('task.id'))
            task = Task.objects.get(id=id)
            task.delete()
            messages.success(request,"successfully delete")
            return render_to_response('home.html', {'form': form})

这就是我的网址。 py

and that is my urls.py

url(r'^task_Delete/$', views.task_Delete, name='task_Delete')

此按钮删除的代码:

<form action="{% url 'task_Delete' %}" method="post" >
               {% csrf_token %}
            <input type="hidden" name="task_id" value="{{task.id}}" />
               <input type="submit" value="delete task">
        </form></td>
  </tr>

当我单击删除时,什么都没发生,我不知道为什么,请提前感谢

when i click on delete nothing happend i don't know why , please help thanks in advance

推荐答案

您的代码中存在各种问题(例如 TaskForm 不是根本不需要),但是如果您更改行

There are various problems in your code (for example the TaskForm is not needed at all) however if you change the line

id = int(request.POST.get('task.id'))

id = int( request.POST.get('task_id'))

该对象可能会被删除;请记住,请求参数的名称将与输入的名称 task_id )相同。我建议您使用适当的CBV(a DeleteView )来执行操作-如果您想要一份缓慢而全面的教程,请推荐这篇文章:> https://spapas.github.io/2018/03/19/comprehensive-django -cbv-guide /

the object will probably be deleted; remember that the request parameter's name will be the same as the name of the input (task_id). I recommend using proper CBVs (a DeleteView) for what you want to do - if you want a slow and comprehensive tutorial on that I recommend this article: https://spapas.github.io/2018/03/19/comprehensive-django-cbv-guide/

这篇关于删除Django中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆