7 django django-templates django-forms
我正在尝试使用FormWizard创建一个简单的Django多页面表单.表格应该做的是以下内容:
在第2步中,我无法确定如何显示访问者在步骤1中输入的信息.我正在发布表单的代码以及用于下面步骤1和2的两个模板:
forms.py
from django import forms
from django.shortcuts import render_to_response
from django.contrib.formtools.wizard import FormWizard
class ContactWizard(FormWizard):
def done(self, request, form_list):
return render_to_response('done.html', {
'form_data': [form.cleaned_data for form in form_list],
})
def get_template(self, step):
return 'buydomain/templates/reg%s.html' % step
class Form1(forms.Form):
firstName = forms.CharField()
lastName = forms.CharField()
class Form2(forms.Form):
message = forms.CharField(widget=forms.Textarea)
Run Code Online (Sandbox Code Playgroud)
第1步的模板:
{% block content %}
<p>Step {{ step }} of {{ step_count }}</p>
<form action="." method="post">
<table>
{{ form }}
</table>
<input type="hidden" name="{{ step_field }}" value="{{ step0 }}" />
{{ previous_fields|safe }}
<input type="submit">
</form>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
第2步的模板:
{% block content %}
<p>Step {{ step }} of {{ step_count }}</p>
{% comment %}
Show values entered into Form1 here !
{% endcomment %}
<form action="." method="post">
<table>
{{ form }}
</table>
<input type="hidden" name="{{ step_field }}" value="{{ step0 }}" />
{{ previous_fields|safe }}
<input type="submit">
</form>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
如果我有点不清楚我想要实现什么,我很抱歉,我希望等待有人提供解决方案.提前致谢.
首先是一些一般性建议 - 如果你不理解如何在Django中使用某些东西,请拉出该文本编辑器并查看代码.它并不总是获得答案的最快方式,但我认为从长远来看它会带来好处.
尝试通过覆盖FormWizard子类中的process_step来向第二步添加一些extra_context.以下是FormWizard(django 1.1)中的注释:
def process_step(self, request, form, step):
"""
Hook for modifying the FormWizard's internal state, given a fully
validated Form object. The Form is guaranteed to have clean, valid
data.
This method should *not* modify any of that data. Rather, it might want
to set self.extra_context or dynamically alter self.form_list, based on
previously submitted forms.
Note that this method is called every time a page is rendered for *all*
submitted steps.
"""
pass
Run Code Online (Sandbox Code Playgroud)
所以在你自己的ContactWizard类中,类似的东西(注意:我没有运行它):
class ContactWizard(FormWizard):
def process_step(self,request,form,step):
if step == 1:
self.extra_context = {'whatever': form.whatever_you_want_from_the_form}
return
else:
return
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4565 次 |
最近记录: |