deleting an object in django

前端 未结 1 673
Happy的楠姐
Happy的楠姐 2021-01-27 08:16

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\':
             


        
1条回答
  •  深忆病人
    2021-01-27 08:45

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

    to

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

    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/

    0 讨论(0)
提交回复
热议问题