Skip to main content
 首页 » 编程设计

python之使用模板自定义 Django FormWizard 步骤

2024年08月19日30Free-Thinker

这是我按照 this 制作的工作 FormWizard和 this

View .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 

网址.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])), 
] 

表单.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) 

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> 

我很难理解如何自定义表单的每个步骤。不幸的是,这方面的帮助很少。我看到了this发布关于创建多个模板的帖子,这很有帮助,但我的主要分歧在于我如何创建这些模板以及如何在每个步骤中实现它们。

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

<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> 

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

谢谢!

请您参考如下方法:

希望你明白了。为了遇到这个问题的任何人,这就是我解决这个问题的方法。我将 {{ 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"/> 

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

class ContactWizard(SessionWizardView): 
    template_name = "contact_form.html"