小编Han*_*nes的帖子

Django表单向导 - 选择取决于第一个表单步骤

我用FormWizard创建了一个2步表单,如下所示:

  • 第一步:询问用户位置
  • 第二步:根据用户位置显示多个搜索结果 ,并将其显示为radioButtons

现在第二种形式取决于第一种形式的输入.一些博客或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 …
Run Code Online (Sandbox Code Playgroud)

django dynamic choice formwizard radio-button

5
推荐指数
2
解决办法
3908
查看次数

Django表单不接受选择作为有效选择。看不清楚为什么

我做了一个小表格,向用户询问某个位置(第一阶段),然后对该位置进行地址编码并要求用户确认位置(第二阶段)。一切正常,但是,当我选择一个选项并尝试提交表单以进入第3阶段时,该表单不接受选择,并发出错误“选择有效的选项”。为什么?

我看不到我在哪里弄错了。请让我知道我做错了。谢谢!

我的forms.py

from django.http import HttpResponseRedirect
from django.contrib.formtools.wizard import FormWizard
from django import forms
from django.forms.widgets import RadioSelect
from geoCode import getLocation

class reMapStart(forms.Form):
    location = forms.CharField()
    CHOICES = [(x, x) for x in ("cars", "bikes")]
    technology = forms.ChoiceField(choices=CHOICES)


class reMapLocationConfirmation(forms.Form):    
   CHOICES = []
   locations = forms.ChoiceField(widget=RadioSelect(), choices = [])

class reMapData(forms.Form):
    capacity = forms.IntegerField()

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) …
Run Code Online (Sandbox Code Playgroud)

django django-formwizard

2
推荐指数
1
解决办法
5198
查看次数