运行 Flask App 时出现内部服务器错误
Internal Server Error when running Flask App
非常感谢您的帮助!我是 Flask 的初学者,用它来构建 API。
当执行下面的代码片段和 运行 vs-code 终端中的代码时,它出现在 运行 http://127.0.0.1:5000/ 上的应用程序中。
但是,当我在终端内点击 URL - 在浏览器中启动 URL 并期望显示 'Hello World' - 它显示 'Internal Server Error'在页面上。然后它会打印“应用程序中的错误:/ [GET] 上的异常”。
代码:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
print('Hello World!')
app.run(port=5000)
航站楼:
WKMGB0671549:REST-APIs josshepp$ python3 app_copy.py
* Serving Flask app "app_copy" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Hello World!
[2019-07-11 08:51:50,920] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
return self.finalize_request(rv)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1967, in finalize_request
response = self.make_response(rv)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2097, in make_response
"The view function did not return a valid response. The"
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
127.0.0.1 - - [11/Jul/2019 08:51:50] "GET / HTTP/1.1" 500 -
您的视图函数没有 return 有效响应。简而言之,return "Hello World!" 而不是打印它:
@app.route('/')
def home():
return 'Hello World!'
Flask 会自动为您将几种类型的 return 值转换为 Response,但您可以阅读它们如何转换 (link):
The return value from a view function is automatically converted into a response object for you. If the return value is a string it’s converted into a response object with the string as response body, a 200 OK status code and a text/html mimetype.
如果视图函数这个词容易混淆,flask 将其描述为(link):
A view function is the code you write to respond to requests to your application. Flask uses patterns to match the incoming request URL to the view that should handle it. The view returns data that Flask turns into an outgoing response.
非常感谢您的帮助!我是 Flask 的初学者,用它来构建 API。
当执行下面的代码片段和 运行 vs-code 终端中的代码时,它出现在 运行 http://127.0.0.1:5000/ 上的应用程序中。
但是,当我在终端内点击 URL - 在浏览器中启动 URL 并期望显示 'Hello World' - 它显示 'Internal Server Error'在页面上。然后它会打印“应用程序中的错误:/ [GET] 上的异常”。
代码:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
print('Hello World!')
app.run(port=5000)
航站楼:
WKMGB0671549:REST-APIs josshepp$ python3 app_copy.py
* Serving Flask app "app_copy" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Hello World!
[2019-07-11 08:51:50,920] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
return self.finalize_request(rv)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1967, in finalize_request
response = self.make_response(rv)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2097, in make_response
"The view function did not return a valid response. The"
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
127.0.0.1 - - [11/Jul/2019 08:51:50] "GET / HTTP/1.1" 500 -
您的视图函数没有 return 有效响应。简而言之,return "Hello World!" 而不是打印它:
@app.route('/')
def home():
return 'Hello World!'
Flask 会自动为您将几种类型的 return 值转换为 Response,但您可以阅读它们如何转换 (link):
The return value from a view function is automatically converted into a response object for you. If the return value is a string it’s converted into a response object with the string as response body, a 200 OK status code and a text/html mimetype.
如果视图函数这个词容易混淆,flask 将其描述为(link):
A view function is the code you write to respond to requests to your application. Flask uses patterns to match the incoming request URL to the view that should handle it. The view returns data that Flask turns into an outgoing response.