Python 带有模板的自定义Django FormWizard步骤
这是我的工作表单向导,我按照 views.pyPython 带有模板的自定义Django FormWizard步骤,python,django,forms,templates,django-formwizard,Python,Django,Forms,Templates,Django Formwizard,这是我的工作表单向导,我按照 views.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):
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
url.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])),
]
forms.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)
联系www.info.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>
{{wizard.form.media}
{{wizard.steps.count}的步骤{wizard.steps.step1}
{%csrf_令牌%}
{{wizard.management_form}
{%if wizard.form.forms%}
{{wizard.form.management_form}
{wizard.form.forms%中窗体的%s}
{{form}}
{%endfor%}
{%else%}
{{wizard.form}
{%endif%}
{%if wizard.steps.prev%}
第一步
上一步
{%endif%}
我很难理解定制表单的每个步骤是如何工作的。不幸的是,在这方面几乎没有什么帮助。我看到了一篇关于创建多个模板的帖子以及类似的帮助,但我的主要分歧在于如何创建这些模板以及如何在每个步骤中实现它们
在正常情况下,我可以这样做
<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>
{%csrf_令牌%}
{{form.first{u name}}
{{form.first{u name.errors}}
{{form.last_name}
{{form.last_name.errors}
{{form.email}
{{form.email.errors}
如何访问每个字段?我可以在哪里添加html和其他信息来帮助进行常规样式设计?我应该如何为每个步骤制作一个这样的产品?我是否应该将html和所有内容复制粘贴到其他“模板”中?如何为每个步骤调用每个模板
谢谢 希望你能找到答案。为了所有遇到这件事的人,我就是这样解决的。我用
for
循环替换了{{wizard.form}
,以手动呈现输入:
<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"/>
class ContactWizard(SessionWizardView):
template_name = "contact_form.html"