Flask: Catch-All URLs

Created on 1 Dec 2015  路  4Comments  路  Source: pallets/flask

Hello.

I am trying to create a Catch-All function which serves every URL. I am following the example from http://flask.pocoo.org/snippets/57/

Here is the code snippet:

from flask import Flask
app = Flask(__name__)

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

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

This code snippet does not catch all of the URLs. Am I missing something here?

Thanks.

Most helpful comment

Is there a way to have a static_url_path which isn't None and a catch-all function working harmoniously? What's the reason for static_url_path interfering with catch-all routing?

All 4 comments

Which url doesn't it catch?

Thanks for the reply, David. I found the cause of my problem.

This is what I was doing originally:

from flask import Flask
app = Flask(__name__, static_url_path='')

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

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

static_url_path='' was breaking the code...
I would not recommend setting static_url_path if you are creating a catch-all function.

Is there a way to have a static_url_path which isn't None and a catch-all function working harmoniously? What's the reason for static_url_path interfering with catch-all routing?

I am waiting for answer to question raised by @FredDavison.

Was this page helpful?
0 / 5 - 0 ratings