带有模板的自定义 Django FormWizard 步骤
Posted
技术标签:
【中文标题】带有模板的自定义 Django FormWizard 步骤【英文标题】:Custom Django FormWizard Steps with Templates 【发布时间】:2018-06-04 16:43:53 【问题描述】:这是我通过关注this 和this 制作的工作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):
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
urls.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)
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和所有内容复制并粘贴到其他“模板”中吗?我如何为每个步骤调用每个模板?
谢谢!
【问题讨论】:
【参考方案1】:希望你想通了。为了遇到这个问题的任何人,这就是我解决这个问题的方法。我用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"/>
您可以为每个表单创建一个模板,然后将其关联到其对应的表单,如docs 中所述,或者如果您希望每个表单使用相同的模板,您只需设置您的@987654325 ContactWizard 类中的 @ 属性:
class ContactWizard(SessionWizardView):
template_name = "contact_form.html"
【讨论】:
以上是关于带有模板的自定义 Django FormWizard 步骤的主要内容,如果未能解决你的问题,请参考以下文章