带有模板的自定义 Django FormWizard 步骤

Mid*_*di_ 2 python forms django templates django-formwizard

这是我按照这个这个制作的工作 FormWizard

视图.py

from django.shortcuts import render
from django.template import RequestContext
from django.http import HttpResponseRedirect
from formtools.wizard.views import SessionWizardView

# Create your views here.
def index(request):
    return render(request, 'wizardApp/index.html')

class ContactWizard(SessionWizardView):
    template_name = "wizardApp/contact_form.html"
    def done(self, form_list, **kwargs):
        process_form_data(form_list)
        return HttpResponseRedirect('../home')

def process_form_data(form_list):
    form_data = [form.cleaned_data for form in form_list]

    print(form_data[0]['subject'])
    print(form_data[0]['info1'])
    print(form_data[0]['info2'])
    print(form_data[1]['sender'])
    print(form_data[1]['info1'])
    print(form_data[1]['info2'])
    print(form_data[2]['message'])
    print(form_data[2]['info1'])
    print(form_data[2]['info2'])

    return form_data
Run Code Online (Sandbox Code Playgroud)

网址.py

from django.conf.urls import url
from wizardApp import views

from wizardApp.forms import ContactForm1, ContactForm2, ContactForm3
from wizardApp.views import ContactWizard

app_name = 'wizardApp'

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^home/$', views.index, name='index'),
    url(r'^admin/', admin.site.urls),
    url(r'^contact/$', ContactWizard.as_view([ContactForm1, ContactForm2, ContactForm3])),
]
Run Code Online (Sandbox Code Playgroud)

表格.py

from django import forms

class ContactForm1(forms.Form):
    subject = forms.CharField(max_length=100)
    info1 = forms.CharField(max_length=100)
    info2 = forms.CharField(max_length=100)

class ContactForm2(forms.Form):
    sender = forms.EmailField()
    info1 = forms.CharField(max_length=100)
    info2 = forms.CharField(max_length=100)

class ContactForm3(forms.Form):
    info1 = forms.CharField(max_length=100)
    info2 = forms.CharField(max_length=100)
    message = forms.CharField(widget=forms.Textarea)
Run Code Online (Sandbox Code Playgroud)

contact_form.html

<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
        {{ wizard.form.media }}
    </head>
<body>

<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>

<form action="/contact/" 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 %}
    </table>
    {% if wizard.steps.prev %}
    <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">first step</button>
    <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">prev step</button>
    {% endif %}
    <input type="submit" value="submit"/>
  </form>

  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我在理解自定义表单的每个步骤如何工作时遇到了很多麻烦。不幸的是,对此几乎没有帮助。我看到了这篇关于创建多个模板的帖子,这很有帮助,但我的主要脱节在于我如何创建这些模板以及如何在每个步骤中实现它们。

在正常情况下,我可以做这样的事情

<form novalidate="novalidate" autocomplete="on" method="POST">
            {% csrf_token %}

            <div class="form-horizontal">
              <div class="form-left">
                {{form.first_name}}
                {{form.first_name.errors}}
              </div>
              <div class="form-right">
                {{form.last_name}}
                {{form.last_name.errors}}
              </div>
            </div>
            <div>
              {{form.email}}
              {{form.email.errors}}
            </div>

            <div>
              <input type="submit" value="Submit">
            </div>

        </form>
Run Code Online (Sandbox Code Playgroud)

我如何访问每个单独的字段?我可以在哪里添加 html 和其他位来帮助设置一般样式?我应该如何为每个步骤制作其中一个?我应该基本上将 html 和所有内容复制并粘贴到其他“模板”中吗?我如何为每个步骤调用每个模板?

谢谢!

Ron*_*eon 5

希望你想通了。为了任何遇到这个问题的人,这就是我解决这个问题的方法。我{{ wizard.form }}for循环替换来手动呈现输入:

<form action="/contact/" 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 %}
        {% for field in wizard.form %}
            <label for="{{ field.id_for_label }}">{{ field.label }}</label>
            {{ field }}
            <span class="message">{{ field.errors }}</span>
        {% endfor %}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">first step</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">prev step</button>
{% endif %}
<input type="submit" value="submit"/>
Run Code Online (Sandbox Code Playgroud)

您可以为每个表单创建一个模板,然后按照文档中的描述将其关联到相应的表单,或者如果您希望每个表单使用相同的模板,您需要做的就是template_name在 ContactWizard 类中设置您的属性:

class ContactWizard(SessionWizardView):
    template_name = "contact_form.html"
Run Code Online (Sandbox Code Playgroud)