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

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