I am creating an example to learn more about using inline forms using SessionWizard. In the end, I want to integrate dynamic forms to add and remove individual forms through a template before submitting. However, when data is not available in the second form, it cannot be checked unlike the usual ModelForm.
Is there a method in SessionWizard that needs to be overridden? Is this something that is inherently handled in Django?
Guides and examples are welcome.
models.py
class Parent(models.Model): name = models.CharField(max_length=256) def __unicode__(self): return name class Child(models.Model): name = models.CharField(max_length=256) parent = models.ForeignKey(Parent) def __unicode__(self): return name
urls.py
test_forms = [['parent', ParentForm],['child', ChildFormSet]] urlpatterns = patterns('example.views', url(r'^$', TestWizard.as_view(test_forms)), )
forms.py
class ParentForm(ModelForm): class Meta: model = Parent class ChildForm(ModelForm): class Meta: model = Child exclude = ('parent',) ChildFormSet = inlineformset_factory(Parent, Child, extra=1) class TestWizard(SessionWizardView): """ This WizardView is used to create multi-page forms and handles all the storage and validation stuff using sessions. """
Master-form.html
{% extends "base.html" %} {% load i18n %} {% block head %} {{ wizard.form.media }} {% endblock %} {% block content %} <p>DEFAULT WIZARD FORM Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p> <form action="" method="post">{% csrf_token %} <table> {{ wizard.management_form }} {% if wizard.form.forms %} {{ wizard.form.management_form }} {% for form in wizard.form.forms %} {{ form }} {% endfor %} {% else %} {{ wizard.form }} {% endif %} </table> {% if wizard.steps.prev %} <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button> <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button> {% endif %} <input type="submit" value="{% trans "submit" %}"/> </form> {% endblock %}
source share