蓝图能有自己的 static 与 templates 吗?两个蓝图都有 index.html 会发生什么?这篇建一个有 admin 与 blog 两个蓝图的应用,逐项实测。
admin root_path = <项目>/admin | template_folder = templates | static_folder = <项目>/admin/static | static_url_path = /static | has_static_folder = True blog root_path = <项目>/blog | template_folder = templates | static_folder = <项目>/blog/static | static_url_path = /blog-assets | has_static_folder = True
上一篇:Blueprint 的 URL 前缀;下一篇:子域名路由。模板加载的更多细节:Flask 模板搜索路径。
url_for('admin.static', filename='admin.css') → /admin/static/admin.css
url_for('blog.static', filename='blog.css') → /blog/blog-assets/blog.css
请求: {'/admin/static/admin.css': 200, '/blog/blog-assets/blog.css': 200, '/static/admin.css': 404, '/admin/static/blog.css': 404}
应用的规则里与静态相关的: ['/static/<path:filename>', '/admin/static/<path:filename>', '/blog/blog-assets/<path:filename>']GET /home → 主应用的 home.html GET /admin/ → admin 蓝图的 index.html GET /blog/ → admin 蓝图的 index.html GET /admin/shared → 应用的 shared.html GET /admin/ns → admin/page.html(带子目录的命名) 模板 blog_only.html 只在 blog 蓝图里,应用里任何视图都能用: blog_only.html
Locating template 'shared.html':
1: trying loader of application 'main'
class: jinja2.loaders.FileSystemLoader
encoding: 'utf-8'
followlinks: False
searchpath:
- <项目>/templates
-> found ('<项目>/templates/shared.html')
2: trying loader of blueprint 'admin' (admin)
class: jinja2.loaders.FileSystemLoader
encoding: 'utf-8'
followlinks: False
searchpath:
- <项目>/admin/templates/admin/ns → admin/page.html(带子目录的命名) | 应用里列出的全部模板名: ['admin/page.html', 'blog_only.html', 'home.html', 'index.html', 'shared.html']