Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何使用忽略静态文件的 Flask 创建一个包罗万象的 url 路由favicon.ico和image.png?
favicon.ico
image.png
例子:
/bZdFFek
/favicon.ico
/of9WfXz
/style.css
Flask / Werkzeug通常会做正确的事情。路由按复杂程度排序,因此最不复杂的路由(如“/favicon.ico”)应始终匹配在全部路由之前:
@app.route("/<short_id>") def view_data(short_id): return "You are viewing short ID: {}".format(short_id) @app.route("/favicon.ico") def favicon(): return send_static_file(FAVICON_PATH)
我认为最好的方法是在路由参数中使用正则表达式。This answer to another question有一个很好的例子说明如何做到这一点:https ://stackoverflow.com/a/5872904/64266