2012-12-24 100 views
5

我正在使用Flask 0.9。我有使用Google App Engine的經驗。燒瓶URL路由:將所有其他URL路由到某個函數

  1. 在GAE中,URL匹配模式在它們出現的順序進行評估,先到先得。 Flask中的情況是否一樣?

  2. 在Flask中,如何編寫一個url匹配模式來處理所有其他無法匹敵的url。在GAE中,您最終只需要輸入/.*,如:('/.*', Not_Found)。如何在Flask中做同樣的事情,因爲Flask不支持Regex。

回答

5

這適用於你的第二個問題。

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('/<path:dummy>') 
def fallback(dummy): 
    return 'This one catches everything else' 

path會抓住一切直到結束。 More about the variable converters