[Answer]-Is there a way to force an immidiate HttpResponse from a class based FormView method (eg. get_initial)?

1👍

The view’s dispatch method is what returns an HttpResponse. You could always raise an exception that won’t be handled elsewhere and catch it there. It should just bubble up until it gets there.

Ex.

#place where you want to raise exception
if condition.is_not_met:
    raise WoahException

#views.py
class MyFormView(views.FormView):
    def dispatch(self, request, *args, **kwargs):
        try:
            return super(MyFormView, self).dispatch(request, *args, **kwargs)
        except WoahException:
            return HttpResponse("Woah, there was an exception")

Leave a comment