Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing static files above parent directory in flask

Tags:

python

html

flask

I am using flask and I have around 6 web apps in my repository and so I am trying to pull out all the mutually used .css and .js files into a library directory so my directory structure looks like this:

lib/
  static/
    css/
      my-css.css
    js/
    images/
      my-image.jpg
webapps/
  webapp_1/
    src/
      webapp_1/
        main.py
        templates/
          base.html
          main.html
          other.html
  webapp_2/
    same as webapp_1 more or less
  .
  .
  .
  etc

So from base.html I need to reference the static directory in some form like so:

<link rel='stylesheet' href='../../../../../lib/static/css/my-css.css'>
<link rel="stylesheet" href="../../../../../lib/static/images/my-image.jpg">

And this works when I have a plain HTML file but not when I use it as a template through Flask. Is there something I need to change or some way I can use these files?

like image 832
clifgray Avatar asked Oct 20 '25 13:10

clifgray


1 Answers

In app.py:

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

Then in your templates:

<link rel='stylesheet' href='/static/css/my-css.css'>
<link rel="stylesheet" href="/static/images/my-image.jpg">
like image 188
dylrei Avatar answered Oct 22 '25 04:10

dylrei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!