Flask 框架怎么处理多级静态目录?

2014-02-17 11:21:34 +08:00
 zjxubinbin
比如静态目录是Project/static
那Project/static/images/1.jpg
Project/static/js/1.js
Project/static/css/1.css这种url怎么处理?
在模版里边直接用url_for是可行的,但是调用的资源多了,代码就很乱。。。
6131 次点击
所在节点    Python
2 条回复
Sight4
2014-02-17 11:43:08 +08:00
可以自己写filter解决,静态的路径可以在req之前在context添加,例如:
def req_ctx_config(app, static_path):
@app.before_request
def create_app_config():
if not hasattr(g, 'static_path'):
g.static_path= static_path
然后你就可以在渲染里面使用g.static_path获得静态根目录了,如果配合filter,可以实现类似
{{g.static_path|to_static_url('images', '1.jpg')}}
这种模式
greatghoul
2014-02-17 11:56:23 +08:00
也可以自己写marco

https://github.com/ScriptFan/ScriptFan.com/blob/dev/website/scriptfan/templates/_helpers.html

然后直接

{{ t.css('lib/bootstrap/css/bootstrap.min.css') }}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/100542

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX