Python Django delete current object

前端 未结 5 458
别那么骄傲
别那么骄傲 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:46

    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()
    
    0 讨论(0)
  • 2020-12-10 19:50

    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')
    
    0 讨论(0)
  • 2020-12-10 19:55
    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.

    0 讨论(0)
  • 2020-12-10 19:57

    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')
    
    0 讨论(0)
  • 2020-12-10 20:07

    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 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>
    
    0 讨论(0)
提交回复
热议问题