Flask blueprint在蓝图而不是根目录下查找静态文件

编程入门 行业动态 更新时间:2024-10-27 19:16:24
本文介绍了Flask blueprint在蓝图而不是根目录下查找静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的蓝图应该在根目录下查找静态文件,但不是。

假设我有一个名为frontend的蓝图。 '前端'只有template_folder ='前端'传入。

即使我把静态文件放在/app/frontend/static/file.css下,找到它。它也没有在/app/static/file.css中找到它。

控制台网络服务器每次都会为每个css或js静态文件显示'404'。

当我打印出url_maps时显示:

< Rule'/ static /< filename>'(HEAD,OPTIONS, GET) - >静态>])

我发起了这样的register_blueprint('frontend')。 $

frontend = Blueprint('frontend',__name __,template_folder ='templates')

我查看的索引返回:

return render_template('frontend.html ',pagination = pagination)

frontend.html 只能在 / app / frontend / templates

中使用 $ b 如果你把前端。 HTML 在 / app / templates 它在那里也不起作用。即使您删除了template_folder参数。

文件结构:

app -app.py --frontend --frontend / templates包含:(frontend.html)(base.html)(errors / 404.html) --static --static / css(bootstrap.min.css)(我想用url_for在base.html中抓取)

解决方案

1。模板

每个瓶子应用程序,蓝图和模块都有 jinja_loader 。当你的 render_template 开始找到你的模板时,它会在应用程序中找到,然后在每个蓝图中查找(参见 get_source and

jinja_loader

这意味着如果您在应用程序模板文件夹中有文件,那么即使您在蓝图中渲染模板,也可以渲染它。另外,如果您在应用程序模板文件夹中没有文件,但在蓝图中的任何位置,则会显示钢。因此,蓝图更好地使用独特的模板前缀来避免模板重写(falsk_admin - admin / 模板前缀)。

扩展的例子有助于在包中使用模板,并且已经有能力在你的应用程序中替换它,如果你需要的话(如果你只是继承你需要给模板的新名称)。 2。 URL规则

规则没有任何蓝图前缀 - 在blueprint中,您只需连接前缀和规则,然后使用它。

$ b $因此,所有的url规则将通过存在的规则来解决(请参阅另一个答案 stackoverflow/a/17146563/880326您可以拥有相同的网址规则描述( / static 和 > / static )并且只有一个端点。
3。静态文件

默认情况下应用程序存在静态文件夹。对于蓝图,您应该添加 static_folder 参数。但是,如果两个规则都具有相同的描述,那么默认情况下您无法从这两个文件中获取文件(可以使用少量代码)。 对于应用程序,蓝图和模块完全静态文件夹依赖形式 root_path ,它依赖于然而,如果你有没有url前缀的蓝图,最好设置另一个 static_url_path 参数。

避免错误。

The blueprint I have should look for static files in the root directory but it isn't.

Say I have a blueprint named 'frontend'. 'frontend' only has template_folder='frontend' passed in.

Even if I put the static files under /app/frontend/static/file.css, it doesn't find it. Neither does it find it in /app/static/file.css.

The console webserver says '404' for every css or js static file every time.

But I do have this showing when I print out url_maps:

<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>])

I initiated register_blueprint('frontend') like that.

frontend = Blueprint('frontend', __name__,template_folder='templates')

My index in view returns:

return render_template('frontend.html', pagination=pagination)

frontend.html can ONLY work in /app/frontend/templates

If you put frontend.html at /app/templates it does not work there either. Even if you remove the "template_folder" param.

File structure:

app -app.py --frontend --frontend/templates contains: (frontend.html) (base.html) (errors/404.html) --static --static/css (bootstrap.min.css) (what I want to grab with url_for inside base.html)

解决方案

1. Templates

Each flask application, blueprint and module has jinja_loader. When your render_template start find your template it find before in application and then in each blueprint (see get_source and _iter_loader), until not find first exist file.

jinja_loader builds from object path and template folder name, for example, if your application, blueprint or module in /home/www/myapp (/usr/lib/python3.4/site-packages/myapp) folder and template folder than full template folder will be /home/www/myapp/template (/usr/lib/python3.4/site-packages/mysapp/tempaltes).

It's mean if you have file in application template folder then you render it even if you render template in blueprint. Also if you don't have file in application template folder but in any in blueprint it steel will be rendered. So for blueprints better use unique template prefix to avoid template overriding (falsk_admin - admin/ template prefix).

For example for extension it helps use templates in packages and already have ability replace it in your application if you need it (if you want just inherit you need give new name for template).

2. URL rules

Rules do not have any blueprints prefixes - in blueprint you just concatenate prefix and rule and then use just it.

So all url rules will resolved with exist rules (see another answer stackoverflow/a/17146563/880326).

You can have same url rule descriptions (/static and /static) and only one endpoint.

3. Static files

For application by default exist static folder. For blueprint you should add static_folder argument. But if both rules will have same descriptions then you can't get files from both by default (with little code can).

For flask application, blueprint and module full static folder dependence form root_path which depends from __name__ argument.

However if you have blueprint without url prefix better set another static_url_path to avoid mistakes.

更多推荐

Flask blueprint在蓝图而不是根目录下查找静态文件

本文发布于:2023-08-02 11:52:21,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:根目录   蓝图   静态   而不是   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!