湿壁画的静态文件处理
fresco-static的Python项目详细描述
基本用法
首先你需要一个壁画应用程序。这是我之前做的一个:
app = FrescoApp()
假设这个应用程序的结构如下:
mypackage ├── __init__.rst ├── app.py ├── static │ └── photo.jpg └── setup.py
拥有应用程序后,您可以创建StaticFiles:
的实例static=StaticFiles(app)
staticfiles自动为路径/static添加路由。 静态文件将在^{tt3}下可用$ 例如/static/mypackage/photo.jpg。您可以更改这些默认值:
static=StaticFiles(app,prefix='/media',route_name='media',cache_max_age=3600)
可以有多个包通过一个静态文件提供文件 不要害怕冲突。
现在可以开始配置静态源目录:
# Mount directory '/www/mysite/htdocs'. The first argument is an arbitrary# name used to identify this source. You can use whatever string you like.static.add_directory('site-htdocs','/site/htdocs',cache_max_age=60)# Serve files located in a 'subdir' directory within the python package# 'mypackage'static.add_package('mypackage','subdir',cache_max_age=86400)
参数cache_max_age指定多长时间(秒) 浏览器和代理可以缓存响应。 为了发展你可能想把这个设为零, 但在生产中你应该 将此值设置为合理值,然后 配置缓存http代理服务器。 添加源目录时,可以忽略此参数,并且默认 (在创建StaticFiles对象时配置) 将改为使用。
static.pathfor为静态内容生成url。 你可能会想包括这个 在模板系统的默认命名空间中。你怎么做取决于 您已经集成了模板系统,但它通常是 像这样:
templating.contextprocessor({'static':static.pathfor})
在模板中调用此项以链接到静态文件,例如:
<!-- Reference a file from the "site-htdocs" source --><imgsrc="{{ static('site-htdocs/photo.jpg') }}"alt="My photo"/><!-- Reference a file from the "mypackage" source --><imgsrc="{{ static('mypackage/photo.jpg') }}"alt="My photo"/><!-- Path doesn't begin with a source name — all sources will be searched for a matching file --><imgsrc="{{ static('cat-pictures/miaow.gif') }}"alt="My photo"/>
0.2(发布日期:2018-08-01)
- 源名称不再需要是python标识符
0.1
- 初始版本