我使用 FormWizard 创建了一个两步表单,如下所示:
- 第一步:询问用户位置
- 第二步: 根据用户位置显示多个搜索结果,并将其显示为单选按钮
现在第二种形式取决于第一种形式的输入。几个博客或 stackoverflow 帖子涵盖了类似的主题,我按照说明进行操作。 但是,应该在 process_step 期间保存的变量不适用于下一个_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/')
绝对感谢任何帮助。
谢谢,
PS:帖子已使用更新的代码进行了更新。