【发布时间】:2017-08-03 16:39:07
【问题描述】:
我正在尝试使用 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 %}
我不确定我是否完全理解模板代码,但我希望有一个按钮可以移动到下一个表单。相反,我只看到了您在屏幕截图中看到的提交按钮。
谁能看到我做错了什么?该表单似乎只呈现第一个表单,无法导航。
【问题讨论】:
标签: django forms django-forms django-formwizard