Skip to content Skip to sidebar Skip to footer

Python Django Delete Current Object

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 to /notes/all. C

Solution 1:

from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse


defdelete(request, id):
    note = get_object_or_404(Note, pk=id).delete()
    return HttpResponseRedirect(reverse('notes.views.notes'))

And in urls.py

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

Make sure that you check the user permissions before deleting an object, you can use the @permission_required decorator https://docs.djangoproject.com/en/1.5/topics/auth/default/#the-permission-required-decorator. If you don't check this an user can delete all notes easily.

Usually it's a good idea to remove objects from the DB using a POST or DELETE request, instead of a GET. Imagine that google-bot crawls your site and visits notes/delete/2.

Solution 2:

You don't need to write these views by hand because django comes with these, and they are called generic views.

For example, the contributed delete view does the following:

  1. Asks the user to confirm that they really want to delete an object.
  2. Deletes the object.
  3. Redirects to a view.

Here is how you would use it:

In your views.py:

from django.views.generic.edit import DeleteView # this is the generic viewfrom django.core.urlresolvers import reverse_lazy
from yourapp.models import Note

classNoteDelete(DeleteView):
    model = Note
    success_url = reverse_lazy('all_notes') # This is where this view will# redirect the user
    template_name = 'delete_note.html'

Create the delete_note.html template, which has only this:

Hey, are you sure you want to delete {{ object.title }}?
<formmethod="post">
  {% csrf_token %}
  <buttontype="submit"class="btn btn-danger">Yeap, I'm sure.</button></form>

In your urls.py:

urlpatterns = patterns('',

  url(r'^all/$', 'note.views.notes', name='all_notes'), # Giving your urls a name# makes it easier to refer# to them later
  url(r'^get/(?P<note_id>\d+)/$', 'note.views.note'), #
  url(r'^language/(?P<language>[a-z\-]+)/$', 'note.views.language'), # 
  url(r'^create/$', 'note.views.create'),
  url(r'^delete/(?P<pk>\d+)/$', 'note.views.NoteDelete.as_view()', name="delete_note"),
  url(r'^search/$', 'note.views.search_titles'),
 )

Now, suppose you want to show a link to delete a note, say in your index.html:

Here are all my notes:
<ul>
{% for note in all_notes %}
   <li>{{ note.title }} - <ahref="{% url 'delete_note' pk=note.pk %}">Delete</a></li>
{% endfor %}
</ul>

Solution 3:

You have to put a variable into url:

url(r'^delete/(?P<id>\d+)/$', 'note.views.delete')

Then view function should be like this:

defdelete(request, id):
    obj = Note.objects.get(pk=id)
    obj.delete()
    return HttpResponseRedirect('/notes/all')

Solution 4:

It's a bit of a micro-optimization, but the answers given already will take two database calls, whereas you can do it in one:

Note.objects.filter(pk=id).delete()

Solution 5:

I think below code will solve the problem

Urls.py

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

Views.py

from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect

defdelete(request, id):
    note = get_object_or_404(Note, pk=id).delete()
    return HttpResponseRedirect('/notes/all')

Post a Comment for "Python Django Delete Current Object"