Han*_*nes 2 django django-formwizard
我做了一个小表格,向用户询问某个位置(第一阶段),然后对该位置进行地址编码并要求用户确认位置(第二阶段)。一切正常,但是,当我选择一个选项并尝试提交表单以进入第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)
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/')
Run Code Online (Sandbox Code Playgroud)
我的urls.py
...
(r'^reMap/$', reMapWizard([reMapStart, reMapLocationConfirmation, reMapData])),
...
Run Code Online (Sandbox Code Playgroud)
第一次提交后,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>
Run Code Online (Sandbox Code Playgroud)
尝试将默认选项设置为所有位置,或者不要将其设置为a ,ChoiceField而是CharField将其设置为ChoiceField覆盖表格的唯一位置。
locations = forms.ChoiceField(widget=RadioSelect(), choices = [..all..])
Run Code Online (Sandbox Code Playgroud)
我猜测选择不会在多个POSTs中持续存在,并且该向导最后会验证所有表单。
即使您的表格对这一步骤有效,但最后仍然无效。
因此,将所有位置添加到原始字段构造函数中,或将其设置为CharField以删除选择的自动验证。
| 归档时间: |
|
| 查看次数: |
5198 次 |
| 最近记录: |