Created
November 22, 2011 05:51
-
-
Save jordanorelli/1385001 to your computer and use it in GitHub Desktop.
context form view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Django 1.3's FormView breaks context objects. Here's a potential solution to that.