1

I'm doing an exercise that consists of a small newspaper, I want to allow only to the Article creator to delete it, otherwise, redirect to the 'home' page. But, that's not working, I'm only being able to raise Http404.

views.py:

class ArticleDeleteView(LoginRequiredMixin, DeleteView):
    model = Article
    template_name = 'article_delete.html'
    success_url = reverse_lazy('article_list')

    def get_object(self, queryset=None):
        """ Hook to ensure object is owned by request.user """
        obj = super(ArticleDeleteView, self).get_object()
        if not obj.author == self.request.user:
            print(redirect('home'))
            raise Http404
        return obj

I tried to:

return redirect('home') 

Instead of "raise Http404" but Django throws this error:

'HttpResponseRedirect' object has no attribute 'delete'

What's the way to solve this behavior and why it's this happening?

Thanks for your help!!


EDIT: (Following @SK. Fazlee Rabby comment, that post put me in the right path)

This works sweet! Thanks, SK. Fazlee Rabby

def dispatch(self, request, *args, **kwargs):
    """ Making sure that only authors can delete Articles """
    obj = self.get_object()
    if obj.author != self.request.user:
        messages.error(request, 'Document not deleted.')
        return redirect('article_list')
    messages.success(request, 'Document deleted.')
    return super(ArticleDeleteView, self).dispatch(request, *args, **kwargs)
2

1 Answer 1

0

First of all If you need change behaving of deleting the object you should overwrite delete method not get_object, get_object just for getting the object (if you raise 404 django handle this error and return http404)

you should write code like this:

class ArticleDeleteView(LoginRequiredMixin, DeleteView):
    model = Article
    template_name = 'article_delete.html'
    success_url = reverse_lazy('article_list')

    def delete(self, request, *args, **kwargs):
        """
        Call the delete() method on the fetched object and then redirect to the
        success URL.
        """
        self.object = self.get_object()
        success_url = self.get_success_url()
        if self.object.author == self.request.user:
            return redirect('confirm_deleting', ) # Also add id of Article

        return redirect('home')


class ConfirmingArticleDeleteView(LoginRequiredMixin, DeleteView):
    model = Article
    template_name = 'article_delete.html' # need change
    success_url = reverse_lazy('article_list')

    def delete(self, request, *args, **kwargs):
        """
        Call the delete() method on the fetched object and then redirect to the
        success URL.
        """
        self.object = self.get_object()
        success_url = self.get_success_url()
        if self.object.author == self.request.user:
            self.object.delete()
            return redirect('home')

        return redirect('home')
1
  • Hi Andrey, this works, but the deletion is a two-step process. When clicking on the first button sends us to the confirmation page. With your code is allowing the user that isn't the author to access the confirmation page, although when clicking redirects to the 'home' page that is the behavior that I wanted to implement. How would you stop that user to access to the confirm delete page? Ta
    – ralfillo
    Commented Oct 5, 2018 at 23:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.