【问题标题】:Django Form doesn't accept selection as valid choice. Can't see whyDjango Form 不接受选择作为有效选择。看不出为什么
【发布时间】:2011-02-19 16:55:28
【问题描述】:

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

我看不出我在哪里犯了错误。请让我知道我做错了什么。谢谢!

我的 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)
            form.fields['locations'].choices = [(x, x) for x in address]
        return super(reMapWizard, self).render_template(request, form, previous_fields, step, context)

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/')

我的 urls.py

...
(r'^reMap/$', reMapWizard([reMapStart, reMapLocationConfirmation, reMapData])),
...

第一次提交后由 Django 为随机位置生成的 html 代码

<form action='.' method='POST'><div style='display:none'>
<input type='hidden' name='csrfmiddlewaretoken' value='0f61c17790aa7ecc782dbfe7438031a8' /></div>
<table>
    <input type="hidden" name="wizard_step" value="1" />
    <input type="hidden" name="0-location" value="market street san francisco" id="id_0-location" /><input type="hidden" name="0-technology" value="car" id="id_0-technology" /><input type="hidden" name="hash_0" value="8a654e29d73f2c2f6660b5beb182f0c8" />
    <tr><th><label for="id_1-locations_0">Locations:</label></th><td><ul class="errorlist"><li>Select a valid choice. Market St, San Francisco, CA, USA is not one of the available choices.</li></ul><ul>

<li><label for="id_1-locations_0"><input checked="checked" type="radio" id="id_1-locations_0" value="Market St, San Francisco, CA, USA" name="1-locations" /> Market St, San Francisco, CA, USA</label></li>
</ul></td></tr>
</table>
<p><input type="submit" value="Submit" /></p>
</form>

【问题讨论】:

    标签: django django-formwizard


    【解决方案1】:

    尝试为您的所有位置设置默认选项不要将其设为ChoiceField,而是设为CharField,并且仅在您覆盖表单的地方将其设置为ChoiceField

    locations = forms.ChoiceField(widget=RadioSelect(), choices = [..all..])
    

    我猜这些选择不会在多个POSTs 中持续存在,并且向导会在最后验证所有表单。

    即使您的表单在该步骤中有效,它最终也会变得无效。

    因此,将所有位置添加到原始字段构造函数或将其设为 CharField 以删除选择的自动验证。

    【讨论】:

      【解决方案2】:

      在宇治的帮助下解决办法:

      用 CharField 初始化类

      class reMapLocationConfirmation(forms.Form):
         locations = forms.CharField()
      

      然后覆盖到选择字段

      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)
      

      【讨论】:

        猜你喜欢
        • 2021-08-03
        • 2017-12-30
        • 2015-07-28
        • 2020-08-07
        • 1970-01-01
        • 2020-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多