我正在使用Flask 0.9。我有使用Google App Engine的經驗。燒瓶URL路由:將所有其他URL路由到某個函數
在GAE中,URL匹配模式在它們出現的順序進行評估,先到先得。 Flask中的情況是否一樣?
在Flask中,如何編寫一個url匹配模式來處理所有其他無法匹敵的url。在GAE中,您最終只需要輸入
/.*
,如:('/.*', Not_Found)
。如何在Flask中做同樣的事情,因爲Flask不支持Regex。
我正在使用Flask 0.9。我有使用Google App Engine的經驗。燒瓶URL路由:將所有其他URL路由到某個函數
在GAE中,URL匹配模式在它們出現的順序進行評估,先到先得。 Flask中的情況是否一樣?
在Flask中,如何編寫一個url匹配模式來處理所有其他無法匹敵的url。在GAE中,您最終只需要輸入/.*
,如:('/.*', Not_Found)
。如何在Flask中做同樣的事情,因爲Flask不支持Regex。
如果您需要處理在服務器上沒有找到的所有URL - 只需創建404 hanlder:
@app.errorhandler(404)
def page_not_found(e):
# your processing here
return result
這適用於你的第二個問題。
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。