Django表单向导-根据第一个表单步骤进行选择

0yg35tkg  于 2023-01-06  发布在  Go
关注(0)|答案(3)|浏览(113)

我已经用窗体向导创建了一个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:发布更新了新代码。

nc1teljy

nc1teljy1#

我认为您可以直接在__init__方法中访问POST字典,因为向导似乎通过get_formPOST传递到每个表单示例中,但由于某种原因,我看不到数据。
与其在这个问题上纠缠太久,我提出的替代方案是使用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)
o2gm4chl

o2gm4chl2#

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

self.storage.get_step_data('0')

这将返回为该特定步骤保存在存储后端中的数据Dict。
在我下面的例子中,第一个表单包含一个“活动”下拉选择,第二个表单包含一个位置选择小部件,它只显示该活动可用的位置。
这在您前进或后退向导时有效-如果您按“prev”,上述答案将不起作用,因为它们仅依赖于向导前进(即,如果您在步骤3上按prev,POST dict将不包含步骤0的数据!)

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
mspsb9vt

mspsb9vt3#

在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)

相关问题