栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Flask开发服务器中静态文件的URL路由冲突

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flask开发服务器中静态文件的URL路由冲突

这是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
或为地图规则设置优先级。解决方案:

  1. Flask
    应用程序设置为
    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>/<var_2>/<var3>/')
  3. 添加自己的转换器并用作
    @app.route('/<no_static:var_1>/<var_2>/<var3>/')
  4. 导入
    werkzeug.routing
    ,创建自己的地图实现,更改
    werkzeug.routing.Map
    为自己的实现,导入
    flask
  5. 在生产环境中使用服务器。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/651828.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号