Flask custom "not found" code

1k views Asked by At

I want to change the default 404 code, when flask doesn't finds the route, to other code. How can I do that?

1

There are 1 answers

3
kishkin On BEST ANSWER

As already said, it is generally not a good idea to redefine the meaning of standard status codes.

Although you can change a status code returned, here's an example:

from flask import Flask

app = Flask(__name__)

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

@app.errorhandler(404)
def page_not_found(error):
    return 'This page does not exist', 777

if __name__ == '__main__':
    app.run()

This will return status code 777 on any page other than /.

Here's a result:

777 status code

More on the topic you can find here.