我已经用窗体向导创建了一个2步窗体,如下所示:
- 第一步:询问用户位置
- 第二步:根据用户位置显示多个搜索结果,并将其显示为单选按钮
现在第二个表单依赖于第一个表单的输入。一些博客或stackoverflow帖子涉及类似的主题,我按照说明操作。然而,在process_step期间应该保存的变量对于next _init_不可用。
如何将变量位置从process_step传递到_init_?
class reMapStart(forms.Form):
location = forms.CharField()
CHOICES = [(x, x) for x in ("cars", "bikes")]
technology = forms.ChoiceField(choices=CHOICES)
class reMapLocationConfirmation(forms.Form):
def __init__(self, user, *args, **kwargs):
super(reMapLocationConfirmation, self).__init__(*args, **kwargs)
self.fields['locations'] = forms.ChoiceField(widget=RadioSelect(), choices=[(x, x) for x in location])
class reMapData(forms.Form):
capacity = forms.IntegerField()
class reMapWizard(FormWizard):
def process_step(self, request, form, step):
if step == 1:
self.extra_context['location'] = form.cleaned_data['location']
def done(self, request, form_list):
# Send an email or save to the database, or whatever you want with
# form parameters in form_list
return HttpResponseRedirect('/contact/thanks/')
任何帮助都是绝对感激的。
谢了H
PS:发布更新了新代码。
3条答案
按热度按时间nc1teljy1#
我认为您可以直接在
__init__
方法中访问POST
字典,因为向导似乎通过get_form
将POST
传递到每个表单示例中,但由于某种原因,我看不到数据。与其在这个问题上纠缠太久,我提出的替代方案是使用
render_template
钩子。o2gm4chl2#
您可以使用存储对象从任何步骤获取数据:
这将返回为该特定步骤保存在存储后端中的数据Dict。
在我下面的例子中,第一个表单包含一个“活动”下拉选择,第二个表单包含一个位置选择小部件,它只显示该活动可用的位置。
这在您前进或后退向导时有效-如果您按“prev”,上述答案将不起作用,因为它们仅依赖于向导前进(即,如果您在步骤3上按prev,POST dict将不包含步骤0的数据!)
mspsb9vt3#
在Yuji的帮助下(谢谢)解决问题后的工作代码是: