我有简单的python应用程序,它使用post方法从html表单提交名称和电话。 我在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>
并拥有我的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()
但是当我提交表单时,我在浏览器中收到以下消息:
内部服务器错误
服务器遇到内部错误但无法完成 你的申请。服务器过载或出现错误 申请。
在我的控制台中我有这个:
> * 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 -
如何解决这个问题?
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 -
答案 0 :(得分:2)
问题是您尚未导入request
。
像这样:
from flask import Flask, request
答案 1 :(得分:0)
放置在路径中的所有变量必须确保将其导入到烧瓶路径页面的顶部。