0

Have a directory structure as follows:

Flask_project
-env
-src
--app2.py
-static
--css
---main.css
-templates
--base.html
--index.html
-app.py

If I load the page using app.py in the main folder, the main.css file is loaded fine using: python app.py. This works from the following file:

from flask import Flask, render_template, url_for # import

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html') 

if __name__ == "__main__":
    app.run(debug=True)

However, if I load app2.py using python3 src\app2.py which is in the \src folder as follows, and redirects the template_folder:

from flask import Flask, render_template, url_for # import

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

@app.route('/')
def index():
    return render_template('index.html') 

if __name__ == "__main__":
    app.run(debug=True)

I am unable to load the css\main.css folder, I get the following error:

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

I don't see why placing the app_whatever.py file in a sub directory (in this case \src) makes it unable to locate the main.css file?

For reference the base.html is as follows:

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

And index.html is:

{% extends 'base.html' %}

{% block head %}

{% endblock %}

{% block body %}

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

1 Answer 1

0

As mentioned in your second question, you need to define the location of your static-folder:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.