Search code examples
pythonflask

How to make a flask route for all pages except /static


I am trying to make a flask route that routes to all pages except those in the /static directory

I tried this code: @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def catch_all(path): return 'You want path: %s' % path but it caused /static not to work


Solution

  • If you want to match any url except /static it would be easiest to define no routes at all but instead use a 404 Not Found error handler to house your code.

    from werkzeug.exceptions import NotFound
    from flask import request
    
    @app.errorhandler(NotFound):
        path = request.path
        return 'You want path: %s' % path