Flask蓝图静态目录不工作?

2024-06-01 08:08:49 发布

您现在位置:Python中文网/ 问答频道 /正文

According to the Flask readme,蓝图静态文件可在blueprintname/static访问。但由于某种原因,它不起作用。

我的蓝图是这样的:

  • app/frontend/views.py

    frontend = Blueprint('frontend', __name__, 
                         template_folder='templates',
                         static_folder='static')
    
    @frontend.route('/') etc...
    
  • app/frontend/js/app.js:我的javascript

  • 在Flask应用程序中注册的蓝图(路线工作和所有内容)

当我转到abc.com/frontend/static/js/app.js时,它只给出404。

当我按照烧瓶自述获取静态文件时:

<script src="{{url_for('frontend.static', filename='js/app.js')}}"></script>

输出是

<script src="/static/js/app.js"></script>

也不管用。我的根文件夹中没有任何内容。

我无法访问蓝图中的任何静态文件!烧瓶听我说应该有用!

admin = Blueprint('admin', __name__, static_folder='static')

By default the rightmost part of the path is where it is exposed on the web. Because the folder is called static here it will be available at the location of the blueprint + /static. Say the blueprint is registered for /admin the static folder will be at /admin/static.


Tags: 文件thenameappflask内容adminis
3条回答

对我来说,它的工作原理是初始化这样的蓝图configuration = Blueprint('configuration', __name__, template_folder='templates',static_folder='static'),然后引用这样的静态文件href="{{ url_for('.static', filename='css/base.css') }}"。在href中静态之前有一个点。

我在static_url_path参数中包含一个参数,以确保蓝图的静态路径与主应用程序的静态路径不冲突。

例如:

admin = Blueprint('admin', __name__, static_folder='static', static_url_path='/static/admin')

你可能注册了你的蓝图坐在你网站的根上:

app.register_blueprint(core, url_prefix='')

但是蓝图中的static视图与所有其他蓝图视图没有区别;它使用该url_prefix值使URL唯一。

corestatic视图也是活动的,因此您现在有两个要处理/static/url的路由。因此,如果注册蓝图时没有URL前缀,则必须为这两个路径中的一个提供唯一的路径。

给蓝图一个自定义的static_url_path值,或者给核心烧瓶app

相关问题 更多 >