Skip to content

Instantly share code, notes, and snippets.

@marazmiki
Created August 21, 2014 03:35
Show Gist options
  • Save marazmiki/957b018716220e6db611 to your computer and use it in GitHub Desktop.
Save marazmiki/957b018716220e6db611 to your computer and use it in GitHub Desktop.
class ExampleFormMultipleView(MultipleFormsView):
template_name = 'preferences/change_group_multiple.html'
forms = {
'profile': ProfileForm,
'avatar': AvatarForm,
'contacts': ContactsForm,
}
def forms_valid(self, forms):
for key, form in forms.items():
# Логика обработки
return redirect(self.get_success_url())
from django.views.generic.edit import FormMixin, ProcessFormView
class MultipleFormsMixin(FormMixin):
form_classes = {}
def get_form_classes(self):
return self.form_classes
def get_forms(self, form_classes):
return {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):
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):
pass
class MultipleFormsView(TemplateResponseMixin, BaseMultipleFormsView):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment