这是werkzeug路线优化功能。见
Map.add,
Map.update并且
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-路径深度。
因为
'/<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)])。有
(0,1)-路径非参数字符串长度
static,
(1, 200)-路径字符串参数最大长度200。
因此,我找不到任何精美的方法来设置自己的
Map实现
Flask.url_map或为地图规则设置优先级。解决方案:
- 将
Flask
应用程序设置为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>/<var_2>/<var3>/')。 - 添加自己的转换器并用作
@app.route('/<no_static:var_1>/<var_2>/<var3>/')。 - 导入
werkzeug.routing
,创建自己的地图实现,更改werkzeug.routing.Map
为自己的实现,导入flask
。 - 在生产环境中使用服务器。



