votes up 5

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
github stars 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):
            
😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 0 votes down

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()

May 27, 2021 cRyp70s answer
cRyp70s 113

Add a possible fix

Please authorize to post fix