0

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>
2
  • GET /static/css/main2.css Is that url correct? Commented Jun 9, 2021 at 23:18
  • Thanks, updated the question as I had changed it to main2.css to see if it was a naming issue but that wasn't it. Commented Jun 9, 2021 at 23:38

2 Answers 2

1

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

app = Flask(__name__, template_folder='../templates', static_folder='../static')
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.