I do this with form_invalid
. Here's how I do it:
from django.views.generic import FormView
class ContextFormView(FormView):
def get(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
context = self.get_context_data(**kwargs)
context['form'] = form
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form, **kwargs)
def form_invalid(self, form, **kwargs):
context = self.get_context_data(**kwargs)
context['form'] = form
return self.render_to_response(context)
You could do the same but for form_valid. Normally the body of form_valid looks like this:
def form_valid(self, form):
return HttpResponseRedirect(self.get_success_url())
You would have to override both post
and form_valid
, because post
calls form_valid
.
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form, **kwargs)
else:
return self.form_invalid(form, **kwargs)
def form_valid(self, form, **kwargs):
# take some other action here
return HttpResponseRedirect(self.get_success_url())
oh and just to clarify, the reason this problem exists is that the ProcessFormView
class's get
method is broken. It normally looks like this:
def get(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
return self.render_to_response(self.get_context_data(form=form))
It just throws the kwargs away (._.)