Flask 开发服务器中静态文件的 URL 路由冲突 [英] URL routing conflicts for static files in Flask dev server
问题描述
我想定义一个包含三个变量组件的 url 规则,例如:
I want to define a url rule with three variable components, like:
@app.route('/<var_1>/<var_2>/<var3>/')
但我发现开发服务器在尝试匹配静态文件之前会评估这些规则.所以像:
But I find that the development server evaluates such rules before trying to match for static files. So anything like:
/static/images/img.jpg
将被我的 url 规则捕获,而不是被转发到内置的静态文件处理程序.有没有办法强制开发服务器先匹配静态文件?
will be caught by my url rule, rather than being forwarded to the built-in static file handler. Is there a way to force the development server to match for static files first?
附言只有当规则有两个以上的变量组件时,这才是一个问题.
P.S. This is only an issue if the rule has more than two variable components.
推荐答案
这是 werkzeug 路由优化功能.参见 Map.add
,Map.update
和 Rule.match_compare_key
:
This is werkzeug route optimization feature. See Map.add
, Map.update
and Rule.match_compare_key
:
def match_compare_key(self):
"""The match compare key for sorting.
Current implementation:
1. rules without any arguments come first for performance
reasons only as we expect them to match faster and some
common ones usually don't have any arguments (index pages etc.)
2. The more complex rules come first so the second argument is the
negative length of the number of weights.
3. lastly we order by the actual weights.
:internal:
"""
return bool(self.arguments), -len(self._weights), self._weights
有 self.arguments
- 当前参数,self._weights
- 路径深度.
There are self.arguments
- current arguments, self._weights
- path depth.
对于 '/
我们有 (True, -3, [(1, 100), (1,100), (1, 100)])
.有 (1, 100)
- 最大长度为 100 的默认字符串参数.
For '/<var_1>/<var_2>/<var3>/'
we have (True, -3, [(1, 100), (1, 100), (1, 100)])
. There are (1, 100)
- default string argument with max length 100.
对于 '/static/
我们有 (True, -2, [(0, -6), (1, 200)])代码>.有
(0, 1)
- 路径非参数字符串长度 static
, (1, 200)
- 路径字符串参数最大长度 200.
For '/static/<path:filename>'
we have (True, -2, [(0, -6), (1, 200)])
. There are (0, 1)
- path non argument string length static
, (1, 200)
- path string argument max length 200.
所以我没有找到任何漂亮的方法来为 Flask.url_map
设置自己的 Map
实现或为地图规则设置优先级.解决方案:
So I don't find any beautiful way to set own Map
implementation for Flask.url_map
or set priority for map rule. Solutions:
- 将
Flask
应用程序设置为app = Flask(static_path='static', static_url_path='/more/then/your/max/variables/path/depth/static')代码>.
- 将
@app.route('/
更改为/ / /') @app.route('/prefix/<;var_1>/
./ /') - 添加自己的转换器并用作
@app.route('/<no_static:var_1>/<var_2>/<var3>/')
. - 导入
werkzeug.routing
,创建自己的地图实现,将werkzeug.routing.Map
改为自己的实现,导入flask
. - 在生产环境中使用服务器.
- Setup
Flask
application asapp = Flask(static_path='static', static_url_path='/more/then/your/max/variables/path/depth/static')
. - Change
@app.route('/<var_1>/<var_2>/<var3>/')
to@app.route('/prefix/<var_1>/<var_2>/<var3>/')
. - Add own converter and use as
@app.route('/<no_static:var_1>/<var_2>/<var3>/')
. - Import
werkzeug.routing
, create own map implementation, changewerkzeug.routing.Map
to own implementation, importflask
. - Use server as on production.
这篇关于Flask 开发服务器中静态文件的 URL 路由冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!