The view function for (request.endpoint!r) did not return a valid response. The function either returned None or ended without a return statement.
Package:
flask
56479

Exception Class:
TypeError
Raise code
"The view function did not return a valid response tuple."
" The tuple must have the form (body, status, headers),"
" (body, status), or (body, headers)."
)
# the body must not be None
if rv is None:
raise TypeError(
f"The view function for {request.endpoint!r} did not"
" return a valid response. The function either returned"
" None or ended without a return statement."
)
# make sure the body is an instance of the response class
if not isinstance(rv, self.response_class):
Links to the raise (1)
https://github.com/pallets/flask/blob/83f7efa04708a22c9701488f0f29b59c2b756bef/src/flask/app.py#L1685Ways to fix
Summary:
A flask view must always return either a string, a tuple in the form:
- (body_string, status_code_int)
- or (body_string, header_dict)
- or (body_string, status_code_int, headr_dict)
- or an instance of flask Response class.
The view must have a return statement and it cannot return integers or other objects not in the above
CODE TO REPRODUCE:
from flask import Flask
app=Flask(__name__)
@app.route("/")
def index():
return 200
@app.route("/err")
def err():
p = 5
@app.route("/err2")
def err2():
return None
if __name__ == "__main__":
app.run()
CODE TO FIX:
from flask import Flask
from flask import Response
app=Flask(__name__)
@app.route("/")
def index():
return "test answer" # must be a string or tuple or Response obj
@app.route("/err")
def err():
p = 5
return Response(status=200) #<--- same fix
@app.route("/err2")
def err2():
return "", 200 #<-- cannot be none
if __name__ == "__main__":
app.run()
Add a possible fix
Please authorize to post fix