from django.http import HttpResponse class FormHandler(HttpResponse): def __init__(self, request, *args, **kwargs): if request.method == 'POST': form = self.form(request.POST) if form.is_valid(): self._update(self.valid(request, form, *args, **kwargs)) else: self._update(self.invalid(request, form, *args, **kwargs)) else: self._update(self.unbound(request, self.form(), *args, **kwargs)) def _update(self, response): if not response: return self._charset = response._charset self._is_string = response._is_string self._container = response._container self._headers.update(response._headers) self.cookies.update(response.cookies) self.status_code = response.status_code ########### # EXAMPLE # ########### class MyFormHandler(FormHandler): form = MyForm def valid(self, request, form): process_form(form) submit(some_data) do(something) return HttpResponse('Everything went OK.') def invalid(self, request, form): return render_to_response('errors_in_form.html', {'form': form}) def unbound(self, request, form): return render_to_response('form_input.html', {'form': form})