gpt4 book ai didi

python - Flask URL 路由 : Route All other URLs to some function

转载 作者:太空狗 更新时间:2023-10-29 17:24:31 25 4
gpt4 key购买 nike

我正在使用 Flask 0.9。我有使用 Google App Engine 的经验。

  1. 在 GAE 中,url 匹配模式按照它们出现的顺序进行评估,先到先得。 Flask 中也是这样吗?

  2. 在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com