Jeeves
Jeeves

Reputation: 434

Flask Python not loading main.css

Have a file structure as follows:

enter image description here

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:

enter image description here

app2.py:

enter image description here

When loading via app.py, everything runs fine and the main.css is found. However, when running app2.py (inside the src directory), flask can't find the main.css and returns an error: "

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

Not sure what's going on as the files are nearly identical.

Index.html

{% extends 'base.html' %}

{% block head %}

{% endblock %}

{% block body %}

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

base.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>

Upvotes: 0

Views: 365

Answers (2)

Tuba Mouqeem
Tuba Mouqeem

Reputation: 1

just remove css/ from this code line it is working in my case

<link rel="stylesheet" href="{{url_for('static', filename='main.css') }}">

Upvotes: 0

Kruspe
Kruspe

Reputation: 645

In this case you also need to pass the static-folder location:

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

Upvotes: 1

Related Questions