Python – Flask Default Route possible?

There is a snippet on Flask’s website about a ‘catch-all’ route for flask. You can find it here.

Basically the decorator works by chaining two URL filters. The example on the page is:

@app.route("https://stackoverflow.com/", defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return 'You want path: %s' % path

Which would give you:

% curl 127.0.0.1:5000          # Matches the first rule
You want path:  
% curl 127.0.0.1:5000/foo/bar  # Matches the second rule
You want path: foo/bar

Leave a Comment