我在同一个应用程序中有两个表单,一个基于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。我不明白为什么?
非常感谢任何帮助或澄清。 谢谢。
答案 0 :(得分:1)
之前我遇到过类似的问题,因为最后的正斜杠丢失了。我怀疑原始海报会在这个较晚的日期做出回应,但是对于任何偶然发现这个问题的人,请仔细检查你的URLconf是否包含一个尾部斜杠,如下所示:
r'^reMap/NewEntry/$'
而不是r'^reMap/NewEntry$'
。