3

I have a simple site project in Django and I have movies lists in man page and movies details in the second page.

I want to add a delete button on movies details tab where user can delete the movies object.

views.py

def movies_list(request):
    return render(request, 'movies.html',{'movies':movies.objects.all()})


def movies_details(request,slug):
    movies_details=MyModel.objects.all()
    det=get_object_or_404(movies_details, slug_name=slug)
    return render(request, 'movies_details.html',{'movies_details':movies_details,'det':det})

what is the better method to do that ?

something like this using new view :

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

urls.py

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

or some like this ?

if request.POST.get('delete'):
    obj.delete()

or use some Django form ?

1
  • 1
    "What is best" is off-topic in this Q&A website because it generates opinion-based answers and flame-wars. Unless you are having trouble just pick the one you like more. That said, you can also use the DELETE HTTP method instead of GET or POST. Commented Oct 4, 2017 at 22:11

1 Answer 1

2

You can use DeleteView functionality of django. I think that will be the better one.

from django.views.generic.edit import DeleteView
from django.urls import reverse_lazy

class DeleteTaskView(DeleteView):
     template_name = 'template/delete.html'
     model = Task
     pk_url_kwarg = 'id'

    def get_success_url(self):
        return reverse_lazy('movies_details.views.movies_details')
Sign up to request clarification or add additional context in comments.

4 Comments

what changes I need to work with your code in my project for this ? on the Task only I use my model name ?and in the template_name = 'template/delete.html' can I use some template like 'movies_details.html' ? and final what I need to write in my html template ?sorry but I am new thanx you
don't work your code I take syntax error at this line pk_url_kwarg = 'id'
@Mar, Sorry for late reply. You can use any name as of template and then in pk_url_kwarg instead of id use whatever you are passing in url.
pk_url_kwarg is for url ?and if that I want to delete is the image how to remove from server I mean the complete delete from media folder how to do this with your code ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.