简体   繁体   English

如何使用 Python Flask 加载共享的 static 文件?

[英]How to load shared static files with Python Flask?

We have a server running a basic LAMP stack.我们有一个运行基本 LAMP 堆栈的服务器。 Our team has a few existing PHP apps, but we're going to use Python/Flask going forward (we're all new to Flask, hence the flurry of dumb questions).我们的团队有一些现有的 PHP 应用程序,但我们将继续使用 Python/Flask(我们都是 Flask 的新手,因此出现了一系列愚蠢的问题)。 The PHP pages use some shared css & js files, which include bootstrap as well as a custom bootstrap extension built by my company. PHP 页面使用一些共享的 css 和 js 文件,其中包括引导程序以及我公司构建的自定义引导程序扩展。 Since these files existed before the switch to Flask, they obviously live outside my Flask app's directory structure.由于这些文件在切换到 Flask 之前就存在,因此它们显然位于我的 Flask 应用程序的目录结构之外。 I'm able to use these shared files by creating symbolic links to them in the Flask app's static directory, but apparently my company's custom bootstrap extension includes some font files that the Flask app can't load:我可以通过在 Flask 应用程序的 static 目录中创建指向它们的符号链接来使用这些共享文件,但显然我公司的自定义引导扩展包含一些字体文件,Z9784E91C7B2657917AFZ26BC8 无法加载:

<my-ip-addr> - - [17/May/2021 09:35:11] "GET /fonts/Neue-haas/display-bold-75.woff2 HTTP/1.1" 404 -
<my-ip-addr> - - [17/May/2021 09:35:11] "GET /fonts/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1" 404 -
<my-ip-addr> - - [17/May/2021 09:35:11] "GET /fonts/Neue-haas/roman-text-55.woff2 HTTP/1.1" 404 -
<my-ip-addr> - - [17/May/2021 09:35:11] "GET /fonts/Neue-haas/display-bold-75.woff HTTP/1.1" 404 -
<my-ip-addr> - - [17/May/2021 09:35:11] "GET /fonts/fontawesome-webfont.woff?v=4.7.0 HTTP/1.1" 404 -
<my-ip-addr> - - [17/May/2021 09:35:11] "GET /fonts/Neue-haas/roman-text-55.woff HTTP/1.1" 404 -
<my-ip-addr> - - [17/May/2021 09:35:11] "GET /fonts/Neue-haas/display-bold-75.ttf HTTP/1.1" 404 -
<my-ip-addr> - - [17/May/2021 09:35:11] "GET /fonts/fontawesome-webfont.ttf?v=4.7.0 HTTP/1.1" 404 -
<my-ip-addr> - - [17/May/2021 09:35:11] "GET /fonts/Neue-haas/roman-text-55.ttf HTTP/1.1" 404 -

The custom bootstrap extension attempts to load these fonts like so: url(../fonts/Neue-haas/roman-text-55.woff2) format("woff2") .自定义引导扩展尝试像这样加载这些 fonts: url(../fonts/Neue-haas/roman-text-55.woff2) format("woff2") I've created a fonts directory in my Flask app's home directory & copied these fonts into it.我在我的 Flask 应用程序的主目录中创建了一个fonts目录并将这些 fonts 复制到其中。 Why can't the Flask app find these fonts?为什么 Flask 应用程序找不到这些 fonts?

This is my base.html template:这是我的base.html模板:

<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
        <title><*some title*></title>
        <!-- Bootstrap core CSS -->
        <link href="{{url_for('static', filename='bootstrap.min.css')}}" rel="stylesheet">
        <link href="{{url_for('static', filename='<*custom-bootstrap-extension-name*>.min.css')}}" rel="stylesheet">
<!  This part I added in response to Jason's comment -->
        <style>
            @font-face {
               font-family: "FontAwesome";
               src: url("/static/fontawesome-webfont.woff2?v=4.7.0");
           }
        </style>
    </head>

    <body class="container">
        
        {{ header | safe }}
        {% block content %}{% endblock %}
        {{ footer | safe }}

    </body>

    <script src="{{url_for('static', filename='jquery.min.js')}}"></script>
    <script src="{{url_for('static', filename='bootstrap.min.js')}}"></script>
    <script src="{{url_for('static', filename='<*custom-bootstrap-extension-name*>.min.js')}}"></script>

</html>

This worked for me:这对我有用:

@font-face {
    font-family: "FontAwesome";
    src: url("/static/fonts/fontawesome-webfont.woff2");
}

I added one of these for each font that wasn't loading.我为每种未加载的字体添加了其中一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM