Python Django delete current object

前端 未结 5 469
别那么骄傲
别那么骄傲 2020-12-10 19:26

Case I am in /notes/get/1/ where id=1 and I have created a \"Delete Note\" link in note.html. I need it to delete the current note from database and app and redirect

5条回答
  •  不思量自难忘°
    2020-12-10 19:57

    I think below code will solve the problem

    Urls.py

    url(r'^delete/(?P\d+)/$','project.app.views.delete'),
    

    Views.py

    from django.shortcuts import get_object_or_404
    from django.http import HttpResponseRedirect
    
    def delete(request, id):
        note = get_object_or_404(Note, pk=id).delete()
        return HttpResponseRedirect('/notes/all')
    

提交回复
热议问题