Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask does not load CSS file..?

So the problem I'm having is that my web app only sometimes loads the css file and it uses the same css file data that was loaded even if I make edits to the css file and even when I delete the file. I have no clue what's going on. I have noticed that when it does load the css correctly the following message is displayed:

127.0.0.1 - - [08/Jun/2015 14:46:19] "GET /static/style.css HTTP/1.1" 200

My style.css file is under a folder named static and in my html file I have

<link type='text/css' href='{{ url_for('static', filename='style.css') }}' rel='stylesheet'>
like image 845
Jaquacky Avatar asked Jun 08 '15 19:06

Jaquacky


People also ask

Why is CSS not showing in Flask?

CSS stylesheets are considered static files. There is no interaction with their code, like there is with HTML templates. Therefore, flask has reserved a separate folder where you should put static files such as CSS, Javascript, images or other files. That folder should be created by you and should be named static.

How do I link a CSS file to a Flask?

To add template files like HTML, CSS, JavaScript in Flask application, you need to maintain a certain folder structure like below: Crate a folder for HTML files inside your root directory. I am giving name of that folder as “ templateFiles “ Create a folder for static files like CSS, images etc.

How do I load a static file into a Flask?

To reference the static files using the url_for() function, pass in the name of the directory – static in this case – and the keyword argument of filename= followed by your static file name. Flask automatically creates a static view that serves static files from a folder named static in your application's directory.

How do you reference a CSS file?

The href Attribute href stands for “hypertext reference”. You use it to specify the location of the CSS file and the file name. It is a clickable link, so you can also hold CTRL and click it to view the CSS file. For example, href="styles.


2 Answers

Flask sets cache control headers on static resources, and your browser will cache those files up to 12 hours.

Your options are:

  • Set the SEND_FILE_MAX_AGE_DEFAULT configuration directive to a shorter value; it is set to 43200 but you can set it to 0 if you really want to.

  • Add a cache buster query parameter to your static URLs; if your static URLs end with ?<somerandomvalue> your browser will reload the resource if that random value ever changes. You could do this by hand each time or you can use this Flask snippet to override the url_for() function to add such a value for you based on the file modification time.

  • Use an incognito browser window (private browsing mode in other browsers) to force your browser to ignore the cache for a specific session. Each time you open a new incognito tab or window, the cache should be invalidated for that session.

like image 153
Martijn Pieters Avatar answered Oct 04 '22 03:10

Martijn Pieters


Please use ctrl + f5 because when you use simple refresh it shows data from cache but when you do ctrl + f5 it render new data

like image 35
dheeraj Avatar answered Oct 04 '22 04:10

dheeraj