11

Here is my project hierarchy:

Project

enter image description here

Here is my HTML file :

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Hello World</title>
        <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='custom.css') }}"/>
    </head>
    <body>
        <h1>Hello World</h1>
    </body>
</html>

My .py flask file :

from flask import Flask, render_template, redirect, json, url_for, request, abort
from pymongo import MongoClient

client = MongoClient()
db = client.web_zoo

app = Flask(__name__)

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

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

And my CSS file :

body {
    color: green;
}
h1 {
    color: orange;
}

The HTML and .py files work fine and the HTML is rendered and displayed on the page. However the CSS doesn't change. It just displays it in normal black text.

NOTE : All those imports are there as I was doing something else. I stripped off everything else and still have this problem.

I can link bootstrap to it, and the CSS works, but not for my own CSS files.

I have looked at and tried these following solutions :

CSS Problems with Flask Web App

Application not picking up .css file (flask/python)

best way to override bootstrap css

It's probably quite obvious but I'm stuck. Any help? Thanks in advance!

EDIT - I have added a picture of the project hierarchy now instead of just a list. As you can see there are some other HTML files in there too, but none are used nor referenced.

A. Lord
  • 157
  • 1
  • 3
  • 10
  • Are you sure that tag gets a correct path to your css file? – Reporter Aug 08 '17 at 12:08
  • Inspect the rendered HTML Code to see the result of `url_for` – Ricardo Aug 08 '17 at 12:11
  • 4
    Have you tried to go to url where your css files and hit F5? e.g. go to http://127.0.0.1:8000/static/custom.css (with your browser) and hit F5. For me that did the job. – Moyote Aug 08 '17 at 12:14
  • @reporter 99% sure yes - I have edited a photo in now. – A. Lord Aug 08 '17 at 12:15
  • @Moyote THAT WORKED BUT WHY?! – A. Lord Aug 08 '17 at 12:16
  • 1
    @A.Lord I don't remeber actually (it was a while ago when i had this issue), but here's an interesting thread: https://stackoverflow.com/questions/21714653/flask-css-not-updating. – Moyote Aug 08 '17 at 12:18
  • @A.Lord do you want to give me +1 from solution? – Moyote Aug 08 '17 at 12:22
  • 1
    @A.Lord https://stackoverflow.com/questions/2263096/css-file-is-not-refreshing-in-my-browser Also this seems to be related to this issue – Moyote Aug 08 '17 at 12:27
  • 1
    Google Chrome stores styles in its own cache. If you make changes to the CSS and try to access the same webpage, the last saved CSS is retained. You have to go directly to the CSS File (accessed over HTTP) and refresh for the changes to be reflected – Mangohero1 Aug 08 '17 at 13:25
  • You shouldn't also need to use `url_for` for linking stylesheets. Relative links works just as fine :P – Mangohero1 Aug 08 '17 at 13:26
  • Wrote up an alternative solution here: https://stackoverflow.com/questions/25034812/use-a-css-stylesheet-on-a-jinja2-template/54026518#54026518 – wcyn Jan 03 '19 at 16:53

1 Answers1

45

Try

CTRL+SHIFT+R

in Chrome to reload the CSS.

On Mac try:

CMD+SHIFT+R

Otherwise right-click on the page in Chrome, select Inspect and select the Console tab to see what errors you get.

M3RS
  • 6,720
  • 6
  • 37
  • 47