【问题标题】:Flask blueprint static directory does not work?Flask蓝图静态目录不起作用?
【发布时间】:2024-04-14 01:25:01
【问题描述】:

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。

当我按照 Flask 自述文件获取我的静态文件时:

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

输出是

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

这也不起作用。我的根 app/static/ 文件夹中没有任何内容。

我无法访问蓝图中的任何静态文件! Flask 读到我说它应该可以工作!

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

默认情况下,路径的最右边部分是它在网络上公开的位置。因为该文件夹在这里被称为静态,它将在蓝图 + /static 的位置可用。假设蓝图已为 /admin 注册,静态文件夹将位于 /admin/static。

【问题讨论】:

  • 您是如何在应用中注册您的蓝图的?
  • 另外值得注意的是,您需要将 JS 文件放在 app/frontend/static/js/app.js 而不是 app/frontend/js/app.js(注意在文件路径中包含 static 文件夹)。

标签: python flask


【解决方案1】:

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

例如:

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

【讨论】:

  • 我实际上不得不做 static_url_path='/admin/static' 而不是 /static/admin
  • Ty 4 static_url_path。对于主静态static_url_path='../static/'
【解决方案2】:

您可能已将蓝图注册为位于您网站的根目录:

app.register_blueprint(core, url_prefix='')

但是蓝图中的static 视图与所有其他蓝图视图没有什么不同;它使用 url_prefix 值使 URL 唯一。

核心static 视图处于活动状态,因此您现在有两条路由要处理/static/ URL。因此,如果您要注册没有 URL 前缀的蓝图,则必须为这两个中的一个指定一个唯一路径。

要么给蓝图一个自定义的static_url_path 值,要么给核心Flask app

【讨论】:

  • 谢谢,这在读我时并不清楚。将静态视为另一个路由 URL 是有道理的。这只是有问题,因为蓝图和核心 Flask 应用程序的默认行为都是使用/static/
【解决方案3】:

它通过像这样初始化蓝图对我有用:

configuration = Blueprint('configuration', __name__, template_folder='templates',static_folder='static')

然后像这样引用静态文件

href="{{ url_for('.static', filename='css/base.css') }}"

在 href 的 static 前面有一个点。

【讨论】:

  • 具有魅力,但我仍然对 Flask 感到困惑。你能推荐一本好书吗?
【解决方案4】:

我正在使用 Flask 蓝图。

我的网络应用 Attendance Manager 有三种类型的用户,管理员、教师和学生。

文件结构的一部分如下所示:

+---app.py
|
|
+---student
|   |   student.py
|   |   __init__.py
|   |   
|   +---static
|   |   +---css
|   |   |       login-page.css
|   |   |       signup-page.css
|   |   |       
|   |   \---js
|   |           firebaseConfig.js
|   |           
|   +---templates
|   |       base.html
|   |       student-login-page.html
|   |       student-signup-page.html

app.py:

app.register_blueprint(student, url_prefix='/student')

学生.py:

student = Blueprint('student', __name__, static_folder='static', template_folder='templates')

学生登录页面.html:

<link rel="stylesheet" type="text/css" href="{{ url_for('student.static', filename='css/login-page.css') }}">

将 'the_name_of_the_blueprint.static' 添加到 student-login-page.html(或您链接 css 的任何位置)中 url_for() 的第一个参数帮助我解决了问题。

在官方文档中找到了解决方案here

This GitHub issue 或this Reddit 帖子如果不能解决您的问题,可能会对您有所帮助。

一切顺利!

【讨论】:

    【解决方案5】:

    我有点晚了,但以前的答案都没有真正帮助我实现我所需要的。我发现我需要做一些“技巧”来使模块的静态文件夹按我的预期工作。 我的应用程序由几个模块组成,但现在假设我们将“应用程序”作为主应用程序文件夹,将“位置”作为模块的文件夹,其中放置了本地“静态”目录(我们需要为此加载静态文件模块)。以下是我所做的:

    1. 在主应用程序的 __init__py 文件中,我将 url_prefix='/' 添加到蓝图的定义中
    2. 在我的模块的 routes.py 文件中,我使用了两个静态参数:static_folder='static''static_url_path='/locations/static' 来声明蓝图
    3. 在 html 模板(扩展主应用程序文件夹中的模板)中,我使用以下内容检索静态 css 文件的路径:{{url_for('locations_bp.static', filename='css/style.css')}}locations_bp 是我的 Bluprint 的“位置”模块名称)

    这样,css 文件被加载为/locations/static/css/style.css,这正是我想要实现的:最重要的是,我使用的 url_prefix 允许我避免在所有模块的路由上使用前缀

    【讨论】:

      【解决方案6】:

      这个问题在我身上发生过好几次了。一旦我添加了static_url_path 并且它起作用了,那么它就在某天突然坏掉了。最终解决方案是

      url_prefix='/admin'
      

      不知道为什么,但 url_prefix=''url_prefix='/' 对我不起作用。

      【讨论】:

        最近更新 更多