简体   繁体   English

运行 Flask 应用程序时出现内部服务器错误

[英]Internal Server Error when running Flask App

Really appreciate your help!真的很感谢你的帮助! I am a beginner to Flask, playing with it to build out APIs.我是 Flask 的初学者,用它来构建 API。

When executing the below code snippet and run the code in vs-code terminal, it appears to run the app on http://127.0.0.1:5000/ .当执行以下代码片段并在 vs-code 终端中运行代码时,它似乎在http://127.0.0.1:5000/上运行该应用程序。

However, when I click-through the URL within the terminal - to launch the URL in the browser and expect to display 'Hello World' - it displays 'Internal Server Error' on the page.但是,当我在终端中单击 URL 以在浏览器中启动 URL 并希望显示“Hello World”时,它会在页面上显示“内部服务器错误”。 It then prints 'ERROR in app: Exception on / [GET].然后打印“应用程序中的错误:/[GET] 上的异常”。

Code:代码:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    print('Hello World!')

app.run(port=5000)

Terminal:终端:

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 -

Your view function does not return a valid response.您的视图函数未返回有效响应。 In short, return "Hello World!"简而言之,返回“Hello World!” instead of printing it:而不是打印它:

@app.route('/')
def home():
    return 'Hello World!'

Flask automatically converts several types of return vales into Response for you, but you can read up on how they convert ( link ): Flask 会自动为您将几种类型的返回值转换为 Response,但您可以阅读它们如何转换(链接):

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.如果返回值是一个字符串,它会被转换为一个响应对象,该字符串作为响应主体、一个 200 OK 状态代码和一个 text/html mimetype。

If the term view function is confusing, it is described by flask as ( link ):如果术语视图函数令人困惑,flask 将其描述为(链接):

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. Flask 使用模式将传入的请求 URL 与应该处理它的视图进行匹配。 The view returns data that Flask turns into an outgoing response.该视图返回 Flask 转换为传出响应的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM