我在同一个应用程序中有两种表单,一种基于 Django FormWizard 和“普通”表单。“正常”表单工作正常,但是当我使用向导创建的表单时,用户会被重定向到正常表单,并且不会进入下一个表单步骤。
向导表单位于 ../app/NewEntry 下。当我遇到表单的第二阶段并单击提交时,我被重定向到 ../app,即使 html 代码显示 action='。' 在 html 文件中。
由于它只发生在第二阶段,我认为它与 render_template 函数有关,但我不明白为什么。
forms.py 看起来像:
1 from django.http import HttpResponseRedirect
2 from django.contrib.formtools.wizard import FormWizard
3 from django import forms
4 from django.forms.widgets import RadioSelect
5 from geoCode import getLocation
6 from renooble.pp.models import Project
7
8 class appStart(forms.Form):
9 location = forms.CharField()
10 CHOICES = [(x, x) for x in ("cars", "bikes")]
11 technology = forms.ChoiceField(choices=CHOICES)
12
13 class appLocationConfirmation(forms.Form):
14 locations = forms.CharField()
15
16 class appData(forms.Form):
17 capacity = forms.IntegerField()
18
19 class appWizard(FormWizard):
20
21 def render_template(self, request, form, previous_fields, step, context=None):
22 if step == 1:
23 location = request.POST.get('0-location')
24 address, lat, lng, country = getLocation(location)
25 form.fields['locations'] = forms.ChoiceField(widget=RadioSelect(), choices = [])
26 form.fields['locations'].choices = [(x, x) for x in address]
27 return super(appWizard, self).render_template(request, form, previous_fields, step, context)
28
29
30 def done(self, request, form_list):
31 # Send an email or save to the database, or whatever you want with
32 # form parameters in form_list
33 return HttpResponseRedirect('/contact/thanks/')
34
35 class reMapRedefine(forms.Form):
36 LIMIT_CHOICES = (
37 ('1', '1'),
38 ('2', '2'),
39 ('30', '30'),
40 )
41 numberLimit = forms.CharField(widget=forms.Select(choices=LIMIT_CHOICES))
42
43 country_list = [('Worldwide', 'Worldwide')]
44 country_list = country_list + [(query.country, query.country) for query in Project.objects.all()]
45 country = forms.ChoiceField(required=False, label='Country', choices=country_list)
urls.py 定义如下
######################################################
(r'^app/$', app_startpage),
(r'^app/NewEntry$', appWizard([appStart, appLocationConfirmation, reMapData])),
######################################################
因此,只要调用 appLocationConfirmation,URL 就会从 /app/NewEntry(向导的 URL)更改为 /app。我不明白为什么?
非常感谢任何帮助或澄清。谢谢你。