182

I am rendering a template, that I am attempting to style with an external style sheet. File structure is as follows.

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /styles
        - mainpage.css

mainpage.html looks like this

<html>
    <head>
        <link rel= "stylesheet" type= "text/css" href= "../styles/mainpage.css">
    </head>
    <body>
        <!-- content --> 

None of my styles are being applied though. Does it have something to do with the fact that the html is a template I am rendering? The python looks like this.

return render_template("mainpage.html", variables..)

I know this much is working, because I am still able to render the template. However, when I tried to move my styling code from a "style" block within the html's "head" tag to an external file, all the styling went away, leaving a bare html page. Anyone see any errors with my file structure?

Zack
  • 13,454
  • 24
  • 75
  • 113

13 Answers13

391

You need to have a 'static' folder setup (for css/js files) unless you specifically override it during Flask initialization. I am assuming you did not override it.

Your directory structure for css should be like:

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /static
        /styles
            - mainpage.css

Notice that your /styles directory should be under /static

Then, do this

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

Flask will now look for the css file under static/styles/mainpage.css

codegeek
  • 32,236
  • 12
  • 63
  • 63
26

In jinja2 templates (which flask uses), use

href="{{ url_for('static', filename='mainpage.css')}}"

The static files are usually in the static folder, though, unless configured otherwise.

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • 1
    This is giving me an error unfortunately. BuildError: ('mainpage.css', {}, None) – Zack Mar 07 '14 at 20:36
  • When I try this, I get the error `Uncaught SyntaxError: unexpected token: 'static'`. I'm specifically trying ``. What am I doing wrong? – Rylan Schaeffer Feb 10 '22 at 23:23
17

Still having problems after following the solution provided by codegeek:
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}"> ?

In Google Chrome pressing the reload button (F5) will not reload the static files. If you have followed the accepted solution but still don't see the changes you have made to CSS, then press ctrl + shift + R to ignore cached files and reload the static files.

In Firefox pressing the reload button appears to reload the static files.

In Edge pressing the refresh button does not reload the static file. Pressing ctrl + shift + R is supposed to ignore cached files and reload the static files. However this does not work on my computer.

8

I have read multiple threads and none of them fixed the issue that people are describing and I have experienced too.

I have even tried to move away from conda and use pip, to upgrade to python 3.7, i have tried all coding proposed and none of them fixed.

And here is why (the problem):

by default python/flask search the static and the template in a folder structure like:

/Users/username/folder_one/folder_two/ProjectName/src/app_name/<static>
 and 
/Users/username/folder_one/folder_two/ProjectName/src/app_name/<template>

you can verify by yourself using the debugger on Pycharm (or anything else) and check the values on the app (app = Flask(name)) and search for teamplate_folder and static_folder

in order to fix this, you have to specify the values when creating the app something like this:

TEMPLATE_DIR = os.path.abspath('../templates')
STATIC_DIR = os.path.abspath('../static')

# app = Flask(__name__) # to make the app run without any
app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=STATIC_DIR)

the path TEMPLATE_DIR and STATIC_DIR depend on where the file app is located. in my case, see the picture, it was located within a folder under src.

you can change the template and static folders as you wish and register on the app = Flask...

In truth, I have started experiencing the problem when messing around with folder and at times worked at times not. this fixes the problem once and for all

the html code looks like this:

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

This the code

Here the structure of the folders

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
8

Add the following to your code:-


 <link
   rel= "stylesheet"
   type= "text/css" 
   href= "{{ url_for('static',filename='name of css file') }}"
  >

if you have linked your css like above and its not working then you can do the following:-


  1. Chrome => ctrl + shift + R
  2. Edge => ctrl + shift + R
  3. Firefox => ctrl + shift + R

loopassembly
  • 2,653
  • 1
  • 15
  • 22
3

One more point to add.Along with above upvoted answers, please make sure the below line is added to app.py file:

app = Flask(__name__, static_folder="your path to static")

Otherwise flask will not be able to detect static folder.

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
3

If any of the above method is not working and your code is perfect then try hard refreshing by pressing ( Ctrl + F5 )for windows and for mac ( hold down Cmd and Shift and then press R [Command + Shift + R ]). It will clear all the caches and then reload file. It worked for me.

Hackytech
  • 317
  • 2
  • 11
2

I'm running version 1.0.2 of flask right now. The above file structures did not work for me, but I found one that did, which are as follows:

     app_folder/ flask_app.py/ static/ style.css/ templates/
     index.html

(Please note that 'static' and 'templates' are folders, which should be named exactly the same thing.)

To check what version of flask you are running, you should open Python in terminal and type the following accordingly:

import flask

flask --version

Imran S M
  • 466
  • 2
  • 9
  • 20
Ly-Bach
  • 21
  • 1
  • 4
1

Weirdly, for me having a "static" folder along with naming my .css file as "main.css" works. If I have any other filename - for instance "styles.css" or "mainstyle.css" the style is not reflected on the page when it renders.

Archit K
  • 11
  • 3
0

The flask project structure is different. As you mentioned in question the project structure is the same but the only problem is wit the styles folder. Styles folder must come within the static folder.

static/styles/style.css
Gopi P
  • 525
  • 9
  • 19
0

One more point to add on to this thread.

If you add an underscore in your .css file name, then it wouldn't work.

SW Jeong
  • 138
  • 1
  • 11
0

If you want to connect the html and css when you are using Blueprints within Flask:

  1. Make sure you have the templates and static folder set: core = Blueprint('core', name, template_folder='templates', static_folder='static')

  2. add the url_for in your href code:

-9

I'm pretty sure it's similar to Laravel template, this is how I did mine.

<link rel="stylesheet" href="/folder/stylesheets/stylesheet.css" />

Referred: CSS file pathing problem

Simple flask application that reads its content from a .html file. External style sheet being blocked?

Community
  • 1
  • 1
nCore
  • 2,027
  • 7
  • 29
  • 57
  • 2
    Not going to down vote you but for your edification, Laravel is PHP and Flask is Python. They do not work in a similar way. They have very different directory structures and behaviors. – Mr. Concolato Mar 15 '17 at 17:09
  • to import static files we need to place those static files into a static folder. The folder structure you have given works with a normal HTML file. but not with Flask – Gopi P Sep 14 '19 at 14:42