2

ユーザーに場所を尋ねる小さなフォームを作成し (第 1 段階)、場所をジオコーディングして、ユーザーに場所の確認を求めます (第 2 段階)。すべて正常に動作しますが、選択肢を選択してフォームを送信して第 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/')

私の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>
4

2 に答える 2

5

デフォルトの選択肢をすべての場所に設定するChoiceField、それをaにせずに、フォームを上書きする場所CharFieldとしてのみ設定してみてください。ChoiceField

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

選択は複数の秒にわたって持続せずPOST、ウィザードは最後にすべてのフォームを検証すると思います。

フォームがその1つのステップに対して有効であっても、最後には無効になります。

したがって、すべての場所を元のフィールドコンストラクターに追加するか、CharFieldにして、選択肢の自動検証を削除します。

于 2011-02-19T17:02:45.073 に答える
2

ユウジの助けの後の解決策:

CharField でクラスを初期化する

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

その後、choicefield に上書きします

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:57:11.737 に答える