5

我使用 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:帖子已使用更新的代码进行了更新。

4

3 回答 3

10

我想你可以POST直接在你的方法中访问字典,__init__因为它看起来像向导通过 传递POST到每个表单实例get_form,但由于某种原因我没有看到数据。

我想出的替代方法是使用render_template钩子,而不是停留太久。

class ContactWizard(FormWizard):
    def done(selef, request, form_list):
        return http.HttpResponse([form.cleaned_data for form in form_list])

    def render_template(self, request, form, previous_fields, step, context=None):
        """
        The class itself is using hidden fields to pass its state, so
        manually grab the location from the hidden fields (step-fieldname)
        """
        if step == 2: 
            locations = Location.objects.filter(location=request.POST.get('1-location'))
            form.fields['locations'].choices = [(x, x) for x in locations]
        return super(ContactWizard, self).render_template(request, form, previous_fields, step, context)
于 2011-02-19T15:28:00.503 回答
6

您可以使用存储对象从任何步骤获取数据:

self.storage.get_step_data('0')

这将返回为该特定步骤保存在存储后端中的数据字典。

在下面的示例中,第一个表单包含一个“活动”下拉选择。然后第二个表单包含一个位置选择小部件,它仅显示可用于该活动的位置。

这在您通过向导前进或后退时有效 - 如果您按“prev”,上述答案将不起作用,因为它们仅依赖于前进的向导(即,如果您按 prev,则 POST dict 将不包含第 0 步的数据在第 3 步!)

def get_form(self, step=None, data=None, files=None):

    form = super(EnquiryWizard, self).get_form(step, data, files)
    #print self['forms']['0'].cleaned_data

    step = step or self.steps.current

    if step == '1':
        step_0_data = self.storage.get_step_data('0')
        activity = Activity.objects.get(pk=step_0_data.get('0-activity'))
        locations = Location.objects.filter(activities=activity)
        form.fields['locations'].widget = forms.CheckboxSelectMultiple(choices=[(x.pk,x.name) for x in locations])

    return form
于 2012-07-22T12:08:09.390 回答
1

在 Yuji 的帮助下(谢谢)解决问题后的工作代码是:

class reMapWizard(FormWizard):

    def render_template(self, request, form, previous_fields, step, context=None):
        if step == 1:
            location = request.POST.get('0-location')
            address, lat, lng, country = getLocation(location)
            form.fields['locations'] = forms.ChoiceField(widget=RadioSelect(), choices = [])
            form.fields['locations'].choices = [(x, x) for x in address]
        return super(reMapWizard, self).render_template(request, form, previous_fields, step, context)
于 2011-02-19T19:49:46.170 回答