【问题标题】:form wizard does not show the next button表单向导不显示下一步按钮
【发布时间】: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


    【解决方案1】:

    按下提交按钮时,如果表单有效,表单向导会将用户移至下一步。

    如果您想更改提交按钮的名称,则可以更改

    <input type="submit">
    

    <input type="submit" value="Next">
    

    【讨论】:

    • 我意识到我有一个copy-paste 错误:super(DummyForm, self).__init__(*args, **kwargs) 应该是 super(OtherForm, self).__init__(*args, ** kwargs)
    【解决方案2】:

    阅读文档后,我总结到this。这意味着在您的urls.py 中您应该更改为:

    urlpatterns = [
        url(r'^contact/$', views.ContactWizard.as_view([DummyForm, OtherForm], instance_dict=[DummyForm(), OtherForm()]))
    ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2014-01-18
      • 1970-01-01
      • 2020-11-29
      • 2020-07-05
      • 2022-09-30
      • 1970-01-01
      相关资源
      最近更新 更多