Flask URL Route: Route All other URLs to some function

Gaby Solis picture Gaby Solis · Dec 24, 2012 · Viewed 15k times · Source

I am working with Flask 0.9. I have experience with Google App Engine.

  1. In GAE, the url match patterns are evaluated in the order they appear, first come first serve. Is it the same case in Flask?

  2. In Flask, how to write a url match pattern to deal with all other unmatched urls. In GAE, you only need to put /.* in the end, like: ('/.*', Not_Found). How to do the same thing in Flask since Flask wont support Regex.

Answer

justinas picture justinas · Dec 24, 2012

This works for your second issue.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'This is the front page'

@app.route('/hello/')
def hello():
    return 'This catches /hello'

@app.route('/')
@app.route('/<path:dummy>')
def fallback(dummy=None):
    return 'This one catches everything else'

path will catch everything until the end. More about the variable converters.