本文介绍了简单的表单提交python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的python应用程序,它使用post方法从html表单提交姓名和电话。我在index.html有这个:
< form action =http://127.0.0.1 :5000 / get_phone /method =POST>
名字:
< input type =textname =firstname>
电话:
< input type =textname =lastname>
< input type =submitvalue =Submit>
< / form>
有我的 getPhone.py
:
from flask import Flask
app = Flask(__ name__)
@ app.route( ''get_phone /',methods = ['POST','GET'])
def get_phone():$ b $如果request.method =='POST':
print :',request.form ['firstname'])
print('Phone:',request.form ['lastname'])
return'看看你的终端!
$ b $ if if __name__ =='__main__':
app.run()
$ b $但是,当我提交我的表单,我在我的浏览器中收到以下消息:
$ b
在我的控制台中,我有这个:
> *运行在http://127.0.0.1:5000/(按CTRL + C退出)
127.0.0.1 - - [04 / Feb / 2015 22:20:04]GET / get_phone / HTTP / 1.1 500 -
127.0.0.1 - - [04 / Feb / 2015 22:20:12]POST / get_phone / HTTP / 1.1500 -
127.0.0.1 - - [04 / Feb / 2015 22:26:22]GET / get_phone / HTTP / 1.1500 -
如何解决这个问题
$ b
UPD:consoleLog with debug = true:
*运行在http://127.0.0.1:5000/上(按CTRL + C退出)
*用stat
重新启动127.0.0.1 - - [04 / Feb / 2015 22:35:30]POST / get_phone / HTTP / 1.1500 -
Traceback(最近一次调用的最后一个):
文件C:\ Python34\lib\site-packages\flask\app.py, 1836行,在__call__
返回self.wsgi_app(environ,start_response)
文件C:\Python34\lib\site-packages\flask\app.py,第1820行,在wsgi_app
响应= self.make_response(self.handle_exception(e))
文件C:\Python34\lib\\ site-packages\flask\app.py,第1403行,在handle_except中
ion
reraise(exc_type,exc_value,tb)
文件C:\Python34\lib\\ \\ site-packages \flask\_compat.py,第33行,重新评估
提高价值
文件C:\Python34\lib\site-packages\flask\app .py,第1817行,在wsgi_app
response = self.full_dispatch_request()
文件C:\Python34\lib\site-packages\flask\app.py,行1477,在full_dispatch
_request
rv = self.handle_user_exception(e)
文件C:\Python34\lib\site-packages\flask\app.py,第1381行,在handle_user_e
xception
reraise(exc_type,exc_value,tb)
文件C:\Python34\lib\site-packages\flask\_compat.py ,第33行,重新
提高价值
文件C:\Python34\lib\site-packages\flask\app.py,1475行,在full_dispatch
_request
rv = self.dispatch_request()
文件C:\Python34\lib\site-packages\flask\app.py,行1461,在dispatch_requ
est
返回self.view_functions [rule.endpoint](** req.view_args)
文件C:\ Users \\\
evernight\Desktop\visa + py\getPhone.py,第6行,在get_phone
if request.method =='POST':
NameError:名称'request'未定义
127.0.0.1 - - [04 / Feb / 2015 22:35: 32]GET / get_phone /?__ debugger __ = yes& cmd = resou $ b $ r rce& f = style.css HTTP / 1.1200 -
127.0.0.1 - - [04 / Feb / 2015 22:35 :32]GET / get_phone /?__ debugger __ = yes& cmd = resou $ b $ r rce& f = jquery.js HTTP / 1.1200 -
127.0.0.1 - - [04 / Feb / 2015 22: 35:33] GET / get_phone /?__ debugger __ = yes& cmd = resou $ b $ r rce& f = debugger.js HTTP / 1.1200 -
127.0.0.1 - - [04 / Feb / 2015 22 :35:34] GET / get_phone /?__ debugger __ = yes& cmd = resou $ b $ r rce& f = ubuntu.ttf HTTP / 1.1200 -
127.0.0.1 - - [04 / Feb / 2015 22:35:34]GET / get_phone /?__ debugger __ = yes& cmd = resou
rce& f = console.png HTTP / 1.1200 -
127.0.0.1 - - [04 / Feb / 2015 22:35:34] GET / get_phone /?__ debugger __ = yes& cmd = resou
rce& f = source.png HTTP / 1.1200 -
127.0.0.1 - - [04 / Feb / 2015 22:35:35] GET / get_phone /?__ debugger __ = yes& cmd = resou
rce& f = console.png HTTP / 1.1200 -
解决方案
问题在于您没有导入请求
。
像这样:
from flask import Flask,请求
I have simple python app which gets name and phone from html form submit using post method.I have this in my index.html:
<form action="http://127.0.0.1:5000/get_phone/" method = "POST">
First name:
<input type="text" name="firstname" >
Phone:
<input type="text" name="lastname" >
<input type="submit" value="Submit">
</form>
And have my getPhone.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/get_phone/', methods=['POST', 'GET'])
def get_phone():
if request.method == 'POST':
print ('First name:', request.form['firstname'])
print ('Phone:', request.form['lastname'])
return 'Take a look at your terminal!'
if __name__ == '__main__':
app.run()
But when I submit my form with I get the following message in my browser:
In my console I have this:
> * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [04/Feb/2015 22:20:04] "GET /get_phone/ HTTP/1.1" 500 -
127.0.0.1 - - [04/Feb/2015 22:20:12] "POST /get_phone/ HTTP/1.1" 500 -
127.0.0.1 - - [04/Feb/2015 22:26:22] "GET /get_phone/ HTTP/1.1" 500 -
How to fix this?
UPD: consoleLog with debug = true:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
127.0.0.1 - - [04/Feb/2015 22:35:30] "POST /get_phone/ HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python34\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python34\lib\site-packages\flask\app.py", line 1403, in handle_except
ion
reraise(exc_type, exc_value, tb)
File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python34\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python34\lib\site-packages\flask\app.py", line 1477, in full_dispatch
_request
rv = self.handle_user_exception(e)
File "C:\Python34\lib\site-packages\flask\app.py", line 1381, in handle_user_e
xception
reraise(exc_type, exc_value, tb)
File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python34\lib\site-packages\flask\app.py", line 1475, in full_dispatch
_request
rv = self.dispatch_request()
File "C:\Python34\lib\site-packages\flask\app.py", line 1461, in dispatch_requ
est
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\nevernight\Desktop\visa + py\getPhone.py", line 6, in get_phone
if request.method == 'POST':
NameError: name 'request' is not defined
127.0.0.1 - - [04/Feb/2015 22:35:32] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:32] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:33] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:34] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:34] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:34] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=source.png HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:35] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=console.png HTTP/1.1" 200 -
解决方案
The issue is that you haven't imported request
.Like this:
from flask import Flask, request
这篇关于简单的表单提交python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!