Created
March 1, 2012 22:06
-
-
Save tkaemming/1953579 to your computer and use it in GitHub Desktop.
django class-based view mixins
This file contains 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.utils.decorators import method_decorator | |
def view_class_decorator(decorator): | |
""" | |
Wraps a class based view dispatcher with the provided decorator. | |
Usage: | |
@view_class_decorator(login_required) | |
class SomethingDetailView(DetailView): | |
... | |
""" | |
def inner(cls): | |
cls.dispatch = method_decorator(decorator)(cls.dispatch) | |
return cls | |
return inner |
This file contains 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.http import HttpResponseRedirect | |
from django.views.generic import TemplateView | |
class MultipleFormView(TemplateView): | |
""" | |
A generic view for handling multiple forms at once that depend on the | |
validity of each other. | |
Instead of using the `form_class` class attribute, specify your forms using | |
the `form_classes` dictionary. The key used for each form will be used as | |
the `prefix` keyword argument to the form's constructor (ensuring that each | |
form only gets the data that it is expecting and form fields don't override | |
each other) as well as the `name` argument to `get_form_kwargs`. | |
""" | |
form_classes = {} | |
def get_success_url(self): | |
raise NotImplementedError | |
def get_form_classes(self): | |
return self.form_classes | |
def get_form_kwargs(self, name): | |
kwargs = {'prefix': name} | |
if self.request.method in ('POST', 'PUT'): | |
kwargs.update({ | |
'data': self.request.POST, | |
'files': self.request.FILES, | |
}) | |
return kwargs | |
def get_form_dict(self, form_classes): | |
return dict([(name, form_class(**self.get_form_kwargs(name))) for \ | |
name, form_class in form_classes.items()]) | |
def forms_valid(self, form_dict): | |
return HttpResponseRedirect(self.get_success_url()) | |
def forms_invalid(self, form_dict): | |
return self.render_to_response(self.get_context_data(forms=form_dict)) | |
def get_context_data(self, **kwargs): | |
return kwargs | |
def get(self, request, *args, **kwargs): | |
form_classes = self.get_form_classes() | |
form_dict = self.get_form_dict(form_classes) | |
return self.render_to_response(self.get_context_data(forms=form_dict)) | |
def post(self, request, *args, **kwargs): | |
form_classes = self.get_form_classes() | |
form_dict = self.get_form_dict(form_classes) | |
if all([form.is_valid() for form in form_dict.values()]): | |
return self.forms_valid(form_dict) | |
else: | |
return self.forms_invalid(form_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment