【问题标题】:Django: Submit points at wrong urlDjango:在错误的网址提交点
【发布时间】:2011-02-20 14:37:26
【问题描述】:

我在同一个应用程序中有两种表单,一种是基于 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。我不明白为什么?

非常感谢任何帮助或澄清。 谢谢。

【问题讨论】:

  • 那是你的完整版reMapLocationConfirmation 吗?我认为我们需要看到这一点才能弄清楚这一点。
  • 是的,它是完整的reMaplocationConfirmation。是的,它是同一个人,只是不同的计算机(电子邮件相同)。对此感到抱歉。
  • 所以,请开始点击回答您问题的答案旁边的复选框。这就是stackoverflow的工作原理。我查看了FormWizard 类,也没有看到任何可以执行重定向的东西。您可以删除 render_template 函数并查看它是否解决了问题吗?在最坏的情况下,我认为它应该只是抛出一个哈希失败错误,但让我们排除它。
  • 我玩过代码,如果我将 reMapWizard 的 urls.py 更改为像 (r'^reMapTest/$', reMapWizard([reMapStart, reMapLocationConfirmation, reMapData])), 这样的第一级链接,表单可以正常工作。我不明白为什么...

标签: django render formwizard django-formwizard


【解决方案1】:

由于缺少最后的正斜杠,我之前遇到过类似的问题。我怀疑原始发布者会在这么晚的日期做出回应,但对于任何偶然发现这个问题的人,请仔细检查您的 URLconf 是否包含尾部斜杠,如下所示:

r'^reMap/NewEntry/$' 而不是r'^reMap/NewEntry$'

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多