Home > Mobile >  Django UpdateView - how to return an HttpResponse instead of a URL from "get_success_url"
Django UpdateView - how to return an HttpResponse instead of a URL from "get_success_url"

Time:01-02

In the get_success_url you are supposed to provide a reverse url, however I want to pass a simple HTTPResponse (without a template), how can I accomplish that with an UpdateView like so?

class SomeView(UpdateView):
    model = MyModel
    form_class = MyModelForm
    template_name = 'form.html'

    def get_success_url(self):
        response = HttpResponse('', status=204)
        return response

CodePudding user response:

You can override the get_success_url method in your view if you want to customize the behaviour of the view. But I would say the best standard way is to use form_valid, which is a method that is called when the form associated with the view is successfully validated. It is responsible for returning the HTTP response to be sent to the client.

class SomeView(UpdateView):
    model = MyModel
    form_class = MyModelForm
    template_name = 'form.html'

    def form_valid(self, form):
        # Save the form data to the database (if applicable)
        form.save()
        # Redirect to a different URL after a successful update
        return HttpResponseRedirect('/success/')
  • Related