我正在使用 Flask 0.9。我有使用 Google App Engine 的经验。
在 GAE 中,url 匹配模式按照它们出现的顺序进行评估,先到先得。 Flask 中也是这样吗?
在 Flask 中,如何编写一个 url 匹配模式来处理所有其他不匹配的 url。在GAE中,你只需要把
/.*
放在最后,比如:('/.*', Not_Found)
。由于 Flask 不支持 Regex,如何在 Flask 中做同样的事情。
最佳答案
这适用于您的第二期。
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('/<first>')
@app.route('/<first>/<path:rest>')
def fallback(first=None, rest=None):
return 'This one catches everything else'
path
将捕获所有内容直到结束。 More about the variable converters .
关于python - Flask URL 路由 : Route All other URLs to some function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14023864/