您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

从没有URL前缀的蓝图中提供静态文件

从没有URL前缀的蓝图中提供静态文件

您不能使用flask机制。

在您的示例中,您将为静态forlder创建3条规则:

Rule "/assets/<path:filename>" for "static" endpoint
Rule "/assets/<path:filename>" for "foo.static" endpoint
Rule "/assets/<path:filename>" for "bar.static" endpoint

当flask将匹配您的URL进行统治时,将进行第一次匹配并返回它。在这种情况下,它返回static端点规则。之后将为此端点调度功能,例如一个文件夹。

PS。我不确定到底是static终点,还是更好的检查Map.update方法

但是您始终可以编写自己的请求描述符,该描述符将查看蓝图文件夹:

class MyApp(Flask):
    def static_dispatchers(self):
        yield super(MyApp, self).send_static_file
        for blueprint in self.blueprints.values():
            yield blueprint.send_static_file

    def send_static_file(self, filename):
        last_exception = None
        for static_dispatcher in self.static_dispatchers():
            try:
                return static_dispatcher(filename)
            except NotFound as e:
                last_exception = e
        raise last_exception

PS。此示例不包括蓝图注册序列,因为它存储在dict中。

但是,如果您有两个同名文件,则将处理第一个文件。例如,如果您具有下一个结构:

/static
/static/app.js "console.log('app');"
/foo/static/app.js "console.log('foo app');"
/foo/static/blue.js "console.log('foo blue');"
/foo/static/foo.js "console.log('foo self');"
/bar/static/app.js "console.log('bar app');"
/bar/static/blue.js "console.log('foo blue');"
/bar/static/bar.js "console.log('bar self');"

并在页面中包含脚本:

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

您将获得下一个结果:

app
app
app
foo blue (or bar blue)
foo blue (or bar blue)
foo self
bar self

对于js和CSS,可以使用相同的路径连接文件,但不能对图像执行此操作。

但是,我更喜欢为每个蓝图使用唯一的URL前缀,因为使用它很简单url_for

其他 2022/1/1 18:50:51 有517人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶