将表单中的数据提交到django视图 [英] Submitting data from a form to django view

查看:126
本文介绍了将表单中的数据提交到django视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开html文件时,它会按预期显示,当我在文本框中输入数据并提交时,它会将我重定向到 localhost / myapp / output / 但为什么是我在未提交的文本框中输入的数据,例如 localhost / myapp / output / data_I_submitted

When I open the html file it displays as expected and when I enter data in the text box and submit, It redirects me to localhost/myapp/output/ but why is the data I enter in the text box not submitted for example like localhost/myapp/output/data_I_submitted

My基本的html文件:

My basic html file:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>db app</title>
</head>
<body>
{% csrf_token %}
<form action="output/" method="post" name="Input">
Data : <input type="text" name="text">
<input type="submit" value="submit">
</form>
</body>
</html>

在我的 app.urls 文件中:

urlpatterns = patterns('',
    url(r'^$',views.index),
    url(r'^output/(?P<text>\w+)/$',views.return_data)
)

最后查看

def return_data(request,text):
    return HttpResponse('entered text ' + text)


推荐答案

如果您的目标只是获取表单上的文字:

If your goal is only getting the text on the form:

将您的观点更改为

def return_data(request):
    return HttpResponse('entered text:' + request.POST['text'])

编辑你的网址

urlpatterns = patterns('',
    url(r'^$', views.index),
    url(r'^output/$', views.return_data)
)

和您的模板

<form action="output/" method="post">
{% csrf_token %}
...
</form>

这篇关于将表单中的数据提交到django视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆