Flask dev服务器中的静态文件的URL路由冲突 [英] URL routing conflicts for static files in Flask dev server

查看:138
本文介绍了Flask dev服务器中的静态文件的URL路由冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用三个变量组件定义一个url规则,比如:

  @ app.route('/< var_1> /< var_2> /< var3> /')

服务器在尝试匹配静态文件之前评估这些规则。所以像这样:

  /static/images/img.jpg 

将被我的url规则所捕获,而不是被转发到内置的静态文件处理程序。有没有办法强制开发服务器先匹配静态文件?



这只是一个问题,如果规则有两个以上的变量组件。

解决方案

这是werkzeug路由优化功能。请参阅 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> /'

code>我们有(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 或设置映射规则的优先级。解决方案:


  1. 设置 Flask application as app = Flask (static_path ='static',static_url_path ='/ more / then / your / max / variables / path / depth / static')

  2. 更改 @ app.route('/< var_1> /< var_2> /< var3> /') @ app.route('/ prefix /< var_1> /< var3> /')

  3. 添加自己的转换器并作为 app.route('/< no_static:var_1> /< var_2> /< var3> /')

  4. 导入 werkzeug.routing ,创建自己的映射实现,将 werkzeug.routing.Map 更改为自己的实现,导入 flask

  5. 在服务器上使用服务器。


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

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 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

There are self.arguments - current arguments, self._weights - path depth.

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.

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.

So I don't find any beautiful way to set own Map implementation for Flask.url_map or set priority for map rule. Solutions:

  1. Setup Flask application as app = Flask(static_path='static', static_url_path='/more/then/your/max/variables/path/depth/static').
  2. Change @app.route('/<var_1>/<var_2>/<var3>/') to @app.route('/prefix/<var_1>/<var_2>/<var3>/').
  3. Add own converter and use as @app.route('/<no_static:var_1>/<var_2>/<var3>/').
  4. Import werkzeug.routing, create own map implementation, change werkzeug.routing.Map to own implementation, import flask.
  5. Use server as on production.

这篇关于Flask dev服务器中的静态文件的URL路由冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆