1

so I'm kinda stumped on this one.

Currently, my flask app is structured like this:

/project_dir/
    /app/
        __init__.py
        /templates/
            base.html
        /static/
            /css/
                main.css
            /js/
                main.js
        routes.py
    /other_code/
        a.py
        b.py
        c.py
    run.py

Now, when I try to do this in base.html:

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

I don't get the css linked to the page.

However, when I move the static folder up one-level into /project_dir/, it works.

How can I configure flask to get the static folder from inside of the app folder?

Inspecting the source of the rendered base.html, the path to the css is generated as this:

<link rel="stylesheet" href="/static/main.css">

Clicking on that href will jsut bring you to a Flask 404.

run.py:

from app import app

if __name__ == '__main__'
app.run(debug=False)

app/_ init _.py:

from flask import Flask

app = Flask('My App', template_folder='app/templates')

from app import routes
4
  • Are you getting jinja2.exceptions.TemplateNotFound? templates and static folder should be at project root folder.
    – arshovon
    Mar 6, 2019 at 4:10
  • 1
    How do you instantiate Flask app? Mar 6, 2019 at 8:07
  • Please show how you are instantiating flask app?
    – Debendra
    Mar 6, 2019 at 8:34
  • I added the source of run.py and app/init.py to my post. The top-level run.py just starts the app variable from init.py.
    – tdotdev
    Mar 6, 2019 at 19:54

0

Your Answer

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

Browse other questions tagged or ask your own question.