Flask dev服务器中的静态文件的URL路由冲突 [英] URL routing conflicts for static files in Flask dev server
问题描述
我想用三个变量组件定义一个url规则,比如:
@ app.route('/< var_1> /< var_2> /< var3> /')
服务器在尝试匹配静态文件之前评估这些规则。所以像这样:
/static/images/img.jpg
将被我的url规则所捕获,而不是被转发到内置的静态文件处理程序。有没有办法强制开发服务器先匹配静态文件?
这只是一个问题,如果规则有两个以上的变量组件。
这是werkzeug路由优化功能。请参阅 有 对于 所以我没有找到任何漂亮的方法来设置自己 I want to define a url rule with three variable components, like: But I find that the development server evaluates such rules before trying to match for static files. So anything like: 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. This is werkzeug route optimization feature. See There are For For So I don't find any beautiful way to set own
这篇关于Flask dev服务器中的静态文件的URL路由冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Map.add
, Map.update
和 Rule.match_compare_key
$ b $ $ $ $ $ $ $ $ $ $ $> $ match $匹配关键字($)
排序的匹配比较键
当前执行:
1.没有任何理由的规则首先出现在
表现的原因,因为我们希望它们匹配得更快,一些
常见的通常不会没有任何参数(索引页等)
2.更复杂的规则是第一个,所以第二个参数是
的负数长度的权重数
3.最后我们订购由实际重量。
:internal:
return bool(self.arguments),-len(self._weights),self._weights
pre>
self.arguments
- 当前参数, self._weights $
'/< var_1> /< var_2> /< var3> /'
(True,-3,[(1,100),(1,100),(1,100)])
。 (1,100)
- 最大长度为100的默认字符串参数。
'/ static /< path:filename>'
我们有(True,-2,[(0,-6),(1,200)]) code>。
(0,1)
- 路径非参数字符串长度 static ,
200)
- 路径字符串参数的最大长度为200.
Map
执行 Flask.url_map
或设置映射规则的优先级。解决方案:
Flask
application as app = Flask (static_path ='static',static_url_path ='/ more / then / your / max / variables / path / depth / static')
。
@ app.route('/< var_1> /< var_2> /< var3> /')
到 @ app.route('/ prefix /< var_1> /< var3> /')
。
app.route('/< no_static:var_1> /< var_2> /< var3> /')
。
werkzeug.routing
,创建自己的映射实现,将 werkzeug.routing.Map
更改为自己的实现,导入 flask
。
@app.route('/<var_1>/<var_2>/<var3>/')
/static/images/img.jpg
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
- current arguments, self._weights
- path depth.'/<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/<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.Map
implementation for Flask.url_map
or set priority for map rule. Solutions:
Flask
application as app = Flask(static_path='static', static_url_path='/more/then/your/max/variables/path/depth/static')
.@app.route('/<var_1>/<var_2>/<var3>/')
to @app.route('/prefix/<var_1>/<var_2>/<var3>/')
.@app.route('/<no_static:var_1>/<var_2>/<var3>/')
.werkzeug.routing
, create own map implementation, change werkzeug.routing.Map
to own implementation, import flask
.