Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FormsView
from django.views.generic.base import View, TemplateResponseMixin
from django.views.generic.edit import FormMixin, ProcessFormView
class MultipleFormsMixin(FormMixin):
"""
A mixin that provides a way to show and handle several forms in a
request.
"""
form_classes = {} # set the form classes as a mapping
def get_form_classes(self):
return self.form_classes
def get_forms(self, form_classes):
return dict([(key, klass(**self.get_form_kwargs())) \
for key, klass in form_classes.items()])
def forms_valid(self, forms):
return super(MultipleFormsMixin, self).form_valid(forms)
def forms_invalid(self, forms):
return self.render_to_response(self.get_context_data(forms=forms))
class ProcessMultipleFormsView(ProcessFormView):
"""
A mixin that processes multiple forms on POST. Every form must be
valid.
"""
def get(self, request, *args, **kwargs):
form_classes = self.get_form_classes()
forms = self.get_forms(form_classes)
return self.render_to_response(self.get_context_data(forms=forms))
def post(self, request, *args, **kwargs):
form_classes = self.get_form_classes()
forms = self.get_forms(form_classes)
if all([form.is_valid() for form in forms.values()]):
return self.forms_valid(forms)
else:
return self.forms_invalid(forms)
class BaseMultipleFormsView(MultipleFormsMixin, ProcessMultipleFormsView):
"""
A base view for displaying several forms.
"""
class MultipleFormsView(TemplateResponseMixin, BaseMultipleFormsView):
"""
A view for displaing several forms, and rendering a template response.
"""
@sjlehtin
Copy link

sjlehtin commented Dec 27, 2012

Hi, do you have a license for this gist? MIT or equivalent would be fantastic :)

@akhildangore
Copy link

akhildangore commented Sep 16, 2013

class BaseMultipleFormsView(MultipleFormsMixin, ProcessMultipleFormsView):
"""
A base view for displaying several forms.
"""

class MultipleFormsView(TemplateResponseMixin, BaseMultipleFormsView):
"""
A view for displaing several forms, and rendering a template response.
"""

what should i put under this classes?

@caesarsol
Copy link

caesarsol commented Oct 31, 2013

fantastic thanks!

use these like this:

class NewUserView(MultipleFormsView):
    """ Get these in template using:
            {{ forms.newuser.as_ul }}
    """
    template_name = 'users/user_form.html'
    form_classes = {
        'newuser': NewUserForm,
        'newidea': NewIdeaForm,
    }

@techoutlooks
Copy link

techoutlooks commented Apr 29, 2017

Loved it !

@fosil
Copy link

fosil commented Jul 7, 2019

Thx for the great solution!
In Django 2.2 (elsewhere I did not test it) will cause an error. The problem is in the get_context_data (self, ** kwargs) method from FormMixin:

if 'form' not in kwargs:
  kwargs ['form'] = self.get_form()

The solution is simple - add to MultipleFormsMixin:

def get_context_data(self, **kwargs):
  if 'forms' not in kwargs:
    kwargs['forms'] = self.get_forms()
  return super().get_context_data(**kwargs)

And even a little detail: import View from django.views.generic.base is unnecessary. There's no use anywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment