Django Multi. Form View
Django class based views for using more than one Form or ModelForm in a single view.
Install
$ pip install django-multi-form-view
Usage
Example)
MultiFormView (class multi_form_view.MultiFormView A single view that can display multiple Django forms. Handles displaying, redisplaying on error, and redirects on form success in.
Extends
- django.views.generic import FormView
Attributes and Methods
-
form_classes
A dictionary containing to forms for the view. -
are_forms_valid()
Check if all forms defined inform_classes
are valid. -
forms_valid()
Redirects to get_success_url(). -
forms_invalid()
Renders a response containing the form errors. -
get()
Render the forms. -
get_context_data()
Adds the results ofget_forms()
to the context dictionary with the key'forms'
. -
get_forms()
. Initializes the forms defined inform_classes
with initial data fromget_initial()
and kwargs from get_form_kwargs(). -
get_form_kwargs()
. Build the keyword arguments required to instantiate the form. -
get_initial()
Returns a copy ofinitial
with empty initial data dictionaries for each form. -
post()
Usesare_forms_valid()
to call eitherforms_valid()
orforms_invalid()
.
Example)
MultiModelFormView (class multi_form_view.MultiModelFormView A single view that can display multiple Django ModelForms. Handles displaying, redisplaying on error, and redirects on form success in.
Extends
- multi_form_view.MultiFormView
Attributes and Methods
-
forms_valid()
Callssave()
on each form. -
get_forms()
. Initializes the forms defined inform_classes
with initial data fromget_initial()
, kwargs from get_form_kwargs() and form instance object fromget_objects()
. -
get_objects()
Returns dictionary with the instance objects for each form. Keys should match the corresponding form.
Demo
$ cd demo
$ pip3 install -r requirements.txt
$ python3 manage.py migrate
$ python3 manage.py runserver
Testing
Install PhantomJS
$ python3 demo/manage.py behave