简体   繁体   English

Flask Python 未加载 main.css

[英]Flask Python not loading main.css

Have a file structure as follows:文件结构如下:

在此处输入图像描述

app.py and app2.py are nearly identical, except app2.py directs to the templates folder due to app2.py being in the \src folder. app.py 和 app2.py 几乎相同,除了 app2.py 指向模板文件夹,因为 app2.py 位于 \src 文件夹中。

app.py:应用程序.py:

在此处输入图像描述

app2.py:应用程序2.py:

在此处输入图像描述

When loading via app.py, everything runs fine and the main.css is found.通过 app.py 加载时,一切运行正常,找到了 main.css。 However, when running app2.py (inside the src directory), flask can't find the main.css and returns an error: "但是,在运行app2.py(src目录内)时,flask找不到main.css并返回错误:“

GET /static/css/main.css HTTP/1.1" 404 -获取 /static/css/main.css HTTP/1.1" 404 -

Not sure what's going on as the files are nearly identical.由于文件几乎相同,因此不确定发生了什么。

Index.html索引.html

{% extends 'base.html' %}

{% block head %}

{% endblock %}

{% block body %}

<h1> Template </h1>
    
{% endblock %}

base.html底座.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="{{url_for('static', filename='css/main.css') }}">
        {% block head %}{% endblock %}
    </head>
    <body>
        {% block body %}
        {% endblock %}
    </body>
</html>

In this case you also need to pass the static -folder location:在这种情况下,您还需要传递static -文件夹位置:

app = Flask(__name__, template_folder='../templates', static_folder='../static')

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

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