表单向导不显示下一步按钮
form wizard does not show the next button
我正在尝试使用 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 %}
我不确定我是否完全理解模板代码,但我希望有一个按钮可以移动到下一个表单。相反,我只看到您在屏幕截图中看到的提交按钮。
谁能看出我做错了什么?该表单似乎只呈现第一个表单,无法导航。
阅读文档,我得出结论 this。这意味着在您的 urls.py
中您应该更改为:
urlpatterns = [
url(r'^contact/$', views.ContactWizard.as_view([DummyForm, OtherForm], instance_dict=[DummyForm(), OtherForm()]))
],
按下提交按钮后,如果表单有效,表单向导会将用户移至下一步。
如果您想更改提交按钮的名称,那么您可以更改
<input type="submit">
至
<input type="submit" value="Next">
我正在尝试使用 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 %}
我不确定我是否完全理解模板代码,但我希望有一个按钮可以移动到下一个表单。相反,我只看到您在屏幕截图中看到的提交按钮。
谁能看出我做错了什么?该表单似乎只呈现第一个表单,无法导航。
阅读文档,我得出结论 this。这意味着在您的 urls.py
中您应该更改为:
urlpatterns = [
url(r'^contact/$', views.ContactWizard.as_view([DummyForm, OtherForm], instance_dict=[DummyForm(), OtherForm()]))
],
按下提交按钮后,如果表单有效,表单向导会将用户移至下一步。
如果您想更改提交按钮的名称,那么您可以更改
<input type="submit">
至
<input type="submit" value="Next">