7

我正在创建一个示例,以了解有关使用 SessionWizard 使用内联表单集的更多信息。最终,我想集成动态表单集,以便在提交之前通过模板添加和删除单个表单。但是,当第二个表单中缺少数据时,它无法像常规 ModelForm 那样进行验证。

SessionWizard 中是否有需要重写的方法?它是在 Django 中固有地处理的吗?

指导和例子将不胜感激。

模型.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

网址.py

test_forms = [['parent', ParentForm],['child', ChildFormSet]]

urlpatterns = patterns('example.views',
    url(r'^$', TestWizard.as_view(test_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.
    """
    #template_name = ''

#    def get_template_names(self):
#        """
#        Returns a list of template names to be used for the request.
#        Overridden TemplateResponseMixin for specifying template for step.
#        """
#        return 'survey/forms/wizard_form.html'
#
#    def get_context_data(self, form, **kwargs):
#        context = super(TestWizard, self).get_context_data(form=form, **kwargs)
#        return context
#
#    def get_form_initial(self, step):
#        """
#        Returns dict (list of key, values) for initial form data.
#        Useful for populating form fields with data from prior form, with extra
#        logic for dealing with formsets.
#        """
#        return self.initial_dict.get(step, {})
#
#    def get_form(self, step=None, data=None, files=None):
#        """
#        Constructs the form for a given step - overridden to add extra arguments
#        """
#        form = super(TestWizard, self).get_form(step, data, files)
#        return form

    def done(self, form_list, **kwargs):
        return render_to_response('survey/thanks.html', {
            'form_data': [form.cleaned_data for form in form_list],
        })

向导-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 %}
4

0 回答 0