V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Stitch
V2EX  ›  Python

Flask 的 route 路径与静态文件路径格式一致时,会造成访问冲突

  •  
  •   Stitch · 2016-06-03 17:51:08 +08:00 · 4241 次点击
    这是一个创建于 2890 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 Flask 时, app.route 的路径与静态文件路径格式一致时,会导致优先加载 route 的内容,静态文件无法加载。

    1 、在 app.route 中定义 route('/string:A/string:B/string:C')的 URL 访问路径为:/A/B/C

    2 、静态文件路径,有一个 js 文件存放路径为: static/function/xxx.js 访问路径为:/static/function/xxx.js

    结果在应用中发现,加载的 js 文件内容,变成了 app.route 返回的内容,导致 js 文件加载失败。

    请问大家遇到过这样的问题么? 如何解决的?

    我的解决方法是不用 route ,使用 url 参数,即 /?A=x&B=y 来变通解决了。

    8 条回复    2016-06-06 21:25:17 +08:00
    SErHo
        1
    SErHo  
       2016-06-03 17:59:44 +08:00   ❤️ 1
    julyclyde
        2
    julyclyde  
       2016-06-03 22:33:36 +08:00
    这个行为是正确的。你不要这么用就能躲过了
    ayaseangle
        3
    ayaseangle  
       2016-06-04 03:39:50 +08:00
    应用服务器不跑静态就行了吧。。。
    guyskk
        4
    guyskk  
       2016-06-04 13:41:42 +08:00
    #1
    3. Add own converter 这条可行
    ChiChou
        5
    ChiChou  
       2016-06-04 14:32:05 +08:00
    你可以考虑在这个 route 前面加前缀跟 static 区分啊
    Stitch
        6
    Stitch  
    OP
       2016-06-05 21:36:02 +08:00
    @SErHo 非常感谢回复。
    解决了这个问题的疑问。

    @ChiChou 的方法也是一个解决的办法,还有另外的变形方法是:
    @app.route(/<para1>-<sub>/<para2>-<sub2>/<para2>)
    总体思路是添加与静态路径不一致的正则规则。
    Stitch
        7
    Stitch  
    OP
       2016-06-05 21:40:13 +08:00
    @ayaseangle
    你是说让 nginx 来跑 静态文件?
    是这个思路么?
    ayaseangle
        8
    ayaseangle  
       2016-06-06 21:25:17 +08:00
    @Stitch 对的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2201 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 10:14 · PVG 18:14 · LAX 03:14 · JFK 06:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.