当我打开 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/