我在django 1.4中使用表单向导来有条件地添加最多七个模型的实例。无论用户完成哪些步骤,我都希望最后一步显示他们输入的所有数据的预览。它不必是一个表单,因为用户可以使用“第一步”或“上一步”按钮返回并修复它们搞砸的任何数据。我还想向用户发送一封包含所有数据的确认电子邮件,我怀疑我提出的任何解决方案都会提供相关数据。
以下是我目前的情况:
# views.py
FORMS = [
('person_application', PersonApplicationForm),
('family_application', FamilyApplicationForm),
('student_application', StudentApplicationForm),
('spouse', SpouseForm),
('child', ChildFormSet),
('roommate', RoommateFormSet),
('preview', Form), # only doing this because I think FormWizard requires a Form subclass for every step, which makes sense
]
TEMPLATES = {
...
'preview': 'preview.html',
}
condition_dict = {
...
'preview': True,
}
class SignupWizard(SessionWizardView):
...
def get_context_data(self, form, **kwargs):
context = super(SignupWizard, self).get_context_data(form=form, **kwargs)
if self.steps.current == 'preview':
context.update({'all_data': self.get_all_cleaned_data()})
return context
# # This is triggering an infinite loop or something because python gets stuck at 100+% cpu and won't stop even when I kill runserver
# def get_form_initial(self, step):
# if step == 'preview':
# return self.get_all_cleaned_data()
# return {}
...
# urls.py
urlpatterns = patterns('app.views',
...
url(r'^start$', SignupWizard.as_view(FORMS, condition_dict=condition_dict, instance_dict=modelform_instances), name='wizard'),
url(r'^thanks$', 'thanks', name='thanks'),
)
正如您所看到的,在某些时候我认为我会尝试使用表单进行预览,因此我尝试重写WizardView.get_form_initial。我想使用WizardView.get_all_cleaned_data()来提供所有数据作为表单的初始字典。但是,正如我在评论中提到的,这导致python卡住了,我不得不手动找到并终止进程以阻止它。
所以现在我想我只是覆盖WizardView.get_context_data()来向包含用户输入的所有数据的模板发送一个额外的上下文变量(再次使用get_all_cleaned_data())。然而,由于几个原因,这将有点复杂。由于任何具有相同名称的模型中的任何字段都会相互影响,因此我必须返回并命名所有模型字段名称。这似乎是不必要的,但无论如何。另外,我的两个表单是ModelFormSets,因此来自它们的数据作为字典列表。没什么大不了的,但它会让模板中的解析变得更加困难。这个问题变得越来越长,但查看数据可能会有所帮助,所以这里是get_all_cleaned_data()当前返回的一个示例(当它被发送到模板时):
{'all_data': {'birthdate': datetime.date(1940, 2, 5),
'building_pref_1': u'NGH4',
'building_pref_2': u'K2',
'city': u'Nashville',
'country': u'',
'email': u'johnny@cash.com',
'first_name': u'Johnny',
u'formset-child': [{'birthdate': datetime.date(2013, 2, 3),
'gender': u'F',
'id': None,
'name': u'Rosanne'},
{'birthdate': datetime.date(2013, 2, 1),
'gender': u'F',
'id': None,
'name': u'Cathy'},
{'birthdate': datetime.date(2013, 2, 5),
'gender': u'F',
'id': None,
'name': u'Cindy'},
{'birthdate': datetime.date(2013, 2, 2),
'gender': u'F',
'id': None,
'name': u'Tara'},
{},
{}],
'furnishing': u'F',
'gender': u'F',
'global_id': u'',
'last_name': u'Cash',
'living_situation': u'SC',
'middle_initial': u'',
'move_in_date': None,
'move_out_date': None,
'name': u'Vivian Liberto',
'phone': u'9891111111',
'smoker_status': u'True',
'state_province': u'TN',
'street_1': u'street',
'street_2': u'',
'student_number': None,
'term': <Term: Summer 2013>,
'type': u'F',
'university_status': u'O',
'university_status_other': u'Travelling musician',
'zip_code': u''},
所以,我的问题是,我是在正确的轨道上还是有更好的方法来做到这一点?例如,我可以使用FormPreview子类作为“预览”步骤的表单,并将FormPreview.done()定义为
def done(self, request, cleaned_data):
pass
这样数据就会传递给FormWizard的最终处理机器(即WizardView.done())?
答案 0 :(得分:1)
我只是覆盖get_template_name
来处理要显示的模板(假设你有一个特殊的'预览'步骤。)
然后我重载get_form
,将每个步骤的数据附加到实例变量。
最后,我重载get_context_data
以始终将该实例变量附加到我的模板上下文。
重载get_form让你在显示预览之前操纵数据。