52

I am making a website using html, css, flask and jinja2.

I have a page working on a flask server, the buttons and labels etc. are displayed, but the css stylesheet I have is not loaded in.

How would I link a stylesheet to a jinja2 template. I have looked around on the internet but cannot find out how.

Here is the css stylesheet link; should I change this, or the python code?

<link rel="stylesheet" type="text/css" href="styles.css">

here is my flask code:

@app.route('/')
def resultstemplate():
    return render_template('questions.html', head='Welcome!')

here are the locations of the files:

/python-code.py /templates/template.html /templates/styles.css

simanacci
  • 2,197
  • 3
  • 26
  • 35
ptolemy0
  • 799
  • 1
  • 5
  • 13

6 Answers6

55

All public files (the ones that are not processed, like templates or python files) should be placed into dedicated static folders. By default, Jinja2 has one static folder called static.

This should fix your problem:

  1. Move /templates/styles.css to /static/styles.css

  2. Update your code with following code, that will be translated into correct file location:

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

More info on static files in Jinja2 is here.

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
  • yes, it sort-of works. The font style and size (and maybe others but I can't tell) have worked but nothing else, so the page is not fully working. – ptolemy0 Jul 30 '14 at 13:00
  • 4
    If I have correctly understood you, CSS is loaded and works, which was covered in this question/answer. If you have other issues, accept this answer, and post them in separate questions. – Andrejs Cainikovs Jul 30 '14 at 15:09
  • yes it works perfectly, sorry, it was looking weird as I made it with Chrome, and was now viewing it firefox; there is now incompatibility. – ptolemy0 Jul 30 '14 at 18:41
  • If you are still having issues with the styling not loading after using the above code, try either clearing the cache of your browser or a 'hard refresh' by pressing `ctrl+shift+r`. – Ambassador Kosh Apr 13 '21 at 14:01
6
<link rel="stylesheet" type="text/css" href="styles.css">

href value must be within quotes.

make sure the file name and path are proper OR try the below

<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}"/>
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
  • yes you are right my code is wrong, but that doesn't make it work; I think the path is right; I have now posted the paths on the question – ptolemy0 Jul 30 '14 at 11:00
2

The order of handler might cause the problems:

url: /stylesheets static_dir: stylesheets
url: /.* script: helloworld.application

will work instead of

url: /.* script: helloworld.application
url: /stylesheets static_dir: stylesheets
fedorqui
  • 275,237
  • 103
  • 548
  • 598
George Nguyen
  • 2,169
  • 1
  • 16
  • 7
1

you should use the super() block style . see the code below:

{% block style %}

{{ super() }}

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

{% endblock %}
Alan
  • 3,307
  • 1
  • 19
  • 22
0

Tried almost every solution on Stack Overflow. It only worked for me when I placed the static folder in the same directory as my run.py file. I changed my folder structure from:

app/
    views
    static
    templates
run.py

To:

app/
    views
    templates
static
run.py

I guess moving the run.py instead would work too. Have a look at this Jinja Templating Tutorial for extra info. Not sure why I had to change the structure for it to work though.

wcyn
  • 3,826
  • 2
  • 31
  • 25
  • Here is a solution that works that allows you to avoid refactoring https://stackoverflow.com/questions/58961132/flask-cant-find-static-folder-when-i-have-set-a-new-root-path – joeyagreco Nov 13 '21 at 21:39
0

To add to what's been said here, be sure to update Jinja2 to v2.10, as lesser versions seem to cause this same bug. Cheers!

jaredgorski
  • 512
  • 4
  • 13