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
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()
You have to put a variable into url:
url(r'^delete/(?P<id>\d+)/$', 'note.views.delete')
Then view function should be like this:
def delete(request, id):
obj = Note.objects.get(pk=id)
obj.delete()
return HttpResponseRedirect('/notes/all')
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse
def delete(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.
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
def delete(request, id):
note = get_object_or_404(Note, pk=id).delete()
return HttpResponseRedirect('/notes/all')
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:
Here is how you would use it:
In your views.py
:
from django.views.generic.edit import DeleteView # this is the generic view
from django.core.urlresolvers import reverse_lazy
from yourapp.models import Note
class NoteDelete(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 }}?
<form method="post">
{% csrf_token %}
<button type="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 }} - <a href="{% url 'delete_note' pk=note.pk %}">Delete</a></li>
{% endfor %}
</ul>