我正在尝试使用django表单向导,其中我有一个表单拆分多个页面。我很想从他们的文档中拼凑出一些东西。所以,目前,我有以下几种形式:
class DummyForm(ModelForm):
class Meta:
model = DummyModel
fields = ['name', 'description', 'time_points']
def __init__(self, *args, **kwargs):
super(DummyForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-sm-2'
self.helper.field_class = 'col-sm-10'
self.helper.layout = Layout(
Field('name'),
Field('description'),
Field('time_points'))
class OtherForm(ModelForm):
class Meta:
model = DummyModel
fields = ['more_text']
def __init__(self, *args, **kwargs):
super(DummyForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-sm-2'
self.helper.field_class = 'col-sm-10'
self.helper.layout = Layout(
Field('More_text'))
我将视图和网址配置如下:
views.py
from django.http import HttpResponseRedirect
class ContactWizard(SessionWizardView):
def get_template_names(self):
return "test.html"
def done(self, form_list, **kwargs):
return HttpResponseRedirect('index')
urls.py
url(r'^contact/$', views.ContactWizard.as_view([DummyForm, OtherForm]))
最后,我的 test.html 模板如下所示:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<p>Step {{ wizard.steps.current }} 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 %}
{% if wizard.steps.prev %}
<button name="wizard_prev_step" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_prev_step" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
</table>
<input type="submit">
</form>
{% endblock %}
我不确定我是否完全理解模板代码,但我期待一个按钮可以移动到下一个表单。相反,我只会在屏幕截图中看到提交按钮。
任何人都能看到我做错了吗?表单似乎只呈现第一个表单,无法导航。
答案 0 :(得分:1)
按下提交按钮后,如果表单有效,表单向导会将用户移动到下一步。
如果您想更改提交按钮的名称,则可以更改
<input type="submit">
到
<input type="submit" value="Next">
答案 1 :(得分:0)
阅读文档,我得出结论this。这意味着在您的urls.py
中,您应该更改为:
urlpatterns = [
url(r'^contact/$', views.ContactWizard.as_view([DummyForm, OtherForm], instance_dict=[DummyForm(), OtherForm()]))
],