when we should use return reverse and when we should use return redirect?

I am new in django. I have little bit confusion about reverse and redirect. In this forms if I replace my sucess url ‘return reverse’ to ‘return redirect’ then it will stop working. Why return redirect not working in this forms? when we should use return reverse and when we should use return redirect?

class BlogListMyAccount(LoginRequiredMixin,FormMixin,DetailView):

      raise_exception = True

      model = Blog

      template_name = 'my-account-blog.html'

      form_class = CommentFrom

      def get_success_url(self):

        return reverse('my-account-blog-details', kwargs={'slug': self.object.slug})

  

      def get_context_data(self, **kwargs):

          data = super(BlogListMyAccount, self).get_context_data(**kwargs)

          data['filter'] = BlogComment.objects.filter(is_published="published")

          data['my_forms'] = CommentFrom(initial={'blog': self.object})

          

          return data

      

      def post(self, request, *args, **kwargs):

        self.object = self.get_object()

        form = self.get_form()

        if form.is_valid():

            messages.add_message(self.request, messages.INFO, 'Your Comment pending for admin approval')

            return self.form_valid(form)

        else:

            messages.add_message(self.request, messages.INFO, 'Somethings Wrong. Please resubmit your commnet')

            return self.form_invalid(form)

      def form_valid(self, form):

        form.save()

        return super(BlogListMyAccount, self).form_valid(form)

The two functions return two completely different results.

The reverse function returns a string - the URL referenced by the given name.

The redirect function returns an HttpResponse object with a status code 302.

Thanks KenWhitesell for your replay. Can you please tell me why ‘return redirect’ not working on above code? if I replace my sucess url ‘return reverse’ to ‘return redirect’ then it will stop working.

What is get_success_url supposed to return from the function?

I am using get_success_url to return back to my details page.

I am not asking how you are using it.

The function, get_success_url, is supposed to return something. What is it supposed to return?

KenWhitesell It’s returning an page where I want to go after perform my task

No, it’s not. Look at the implementation of that function and look at what it’s returning.
I’m talking about that specific function within the class - not what you think it’s doing.

1 Like