Django SessionWizardView在提交时重新加载相同的页面

时间:2015-06-07 05:45:37

标签: python django django-formwizard

我使用单个模板可以正常工作,但后来我为不同的表单编写了不同的html,扩展了一个共同的基础html,在提交最后一个表单的同时,向导重新加载了最后一个表单并且没有执行done方法。

继承人我的url.py:

url(r'^settings/new/$', settings_view, name = 'new_settings'),

查看

FORMS = [
    ("Basic", BasicSettingsForm),
    ("Calculation", CalculationForm)
]

TEMPLATES = {
    "Basic":"settings/basic-settings.html",     
    "Calculation":"settings/calculation_settings_form.html"
}

class WizardView(SessionWizardView):

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def get_form_instance(self, step):
        return self.instance_dict.get(step, None)

    def get_form(self, step=None, data=None, files=None):
        form = super(WizardView, self).get_form(step, data, files)
        return form

    def done(self, form_list, **kwargs):
        b_form = form_list[0]
        c_form = form_list[1]
        try: 
            b_data = b_form
            b_data.save()
            c_data = c_form
            c_data.save()
            message = ''
        except Exception as e:
            print e
            message = "Could not save the settings"
        return message_view(self.request, message = message)

@login_required
def attendance_settings_view(request):
    attWizardView = WizardView.as_view(FORMS)
    return attWizardView(request)

base.html文件

{% load i18n %} 
<form id="myForm" action="" method="POST">{% csrf_token %} 
{% block wizardform %}

{% endblock %}
{% if wizard.steps.prev %}
     <button class="btn btn-small btn-primary" name="wizard_goto_step" type="submit"
     value="{{ wizard.steps.prev }}">{% trans "Back" %}</button>
{% endif %} 
{% if wizard.steps.current == wizard.steps.last %}
    <input class="btn btn-small btn-primary" type="submit" value="{% trans " Finish " %}"/>
{% else %}
    <input type="submit" value="{% trans " Next " %}" class="btn btn-small btn-primary">
{% endif %}
    {{ wizard.management_form }}
</form>

{% endblock %}

1 个答案:

答案 0 :(得分:0)

如果表单未正确验证,通常会发生这些错误。我通常包括所有字段并测试它,然后再调整字段(例如隐藏一些字段)。