【问题标题】:Django form processingDjango 表单处理
【发布时间】:2015-07-04 04:51:15
【问题描述】:

我正在使用https://docs.djangoproject.com/en/1.8/topics/forms/ 的表单教程。我正在尝试制作一个简单的表单元素,它将发布一些数据。这是在 urls.py 中启动的函数:

def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
    # create a form instance and populate it with data from the request:
    form = NameForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
        # process the data in form.cleaned_data as required
        # ...
        # redirect to a new URL:
        return HttpResponseRedirect('/thanks/')

# if a GET (or any other method) we'll create a blank form
else:
    form = NameForm()

return render(request, 'name.html', {'form': form})

HTML 代码如下:

<form action="/your-name/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>

当我输入一些数据以形成并单击“提交”按钮时,连接到 /your-name/ URL 的函数会启动并且我被重定向到 /your-name/。我想知道为什么连接到 /thanks/ URL 的函数没有启动。如何到达?

【问题讨论】:

  • 您有拼写错误,我没有看到或理解您的问题。
  • 什么?您已将表单设置为指向 /your-name/,所以这当然是要调用的 URL。如果你想调用/thanks/,你应该把它指向那个。
  • 好的,但是form.is_valid()函数什么时候启动并执行HTTP重定向?

标签: python django django-forms


【解决方案1】:

要使视图起作用,表单的操作/your-name/ 必须由您的视图get_name 提供服务。

您没有显示您的网址格式,因此无法判断是否存在错误。

【讨论】:

    猜你喜欢
    • 2018-01-07
    • 2014-08-23
    • 2021-05-31
    • 2012-02-19
    • 2014-05-08
    • 1970-01-01
    • 2021-10-02
    • 2020-12-03
    • 1970-01-01
    相关资源
    最近更新 更多