python - 从表单提交数据到 Django View

标签 python django html

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

我的基本 html 文件:

<!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)
)

最后是 View :

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

最佳答案

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

改变你的看法

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>

关于python - 从表单提交数据到 Django View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19688047/

相关文章:

python - 在 wx 中使用 matplotlib 显示 numpy 数组

python - Css 文件未与 Django 中的元素链接

python - Django:AppRegistryNotReady()

python - django.db.utils.IntegrityError : NOT NULL constraint failed: unesco_site. 类别_id

javascript - 通过 AJAX 将 HTML 转换为 PHP 表单输入

javascript - 重新创建此预加载屏幕——看起来像 gif 而不是编码动画

python - 大数据集使python脚本崩溃

Python - 并行解压 .gz 文件

python - 通过 django 中的 ssl 证书进行用户身份验证

html - DIV 应在溢出 :hidden parent 内并排保持