Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask - Getting a 304 status for my external .css file

I'm building a web app. All the external links worked in my Project directory worked fine before but I noticed that yesterday every time I modify the .css file it didn't render this change at all. It actually is freezed with the same style regardless if I even erase the whole .css file content.

This is the response that I'm getting when I run flask:

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 230-950-485
127.0.0.1 - - [09/Mar/2017 17:53:09] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2017 17:53:10] "GET /static/css/main.css HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2017 17:53:10] "GET /static/js/main.js HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2017 17:53:10] "GET /static/css/main.css HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2017 17:53:14] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [09/Mar/2017 17:54:04] "GET / HTTP/1.1" 200 -

Notice the 304, seems like that might be the problem? I appreciate any advice in what steps to take.

like image 777
tadm123 Avatar asked Mar 09 '17 23:03

tadm123


People also ask

What is Flask 304 error?

HTTP response 304 is for "Redirection to a previously cached result". This means Flask is telling your browser that it already has the content. Clear your Browser cache and you will notice that Flask returns a 200 on your next request.

Where are CSS files stored 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 to handle external CSS files with flask?

When handling external CSS files with Flask, create index.html at templates folder, and create CSS file at static/css folder, then import CSS file from index.html. Folder name of templates and static are auto included by default settings (possible to change), templates and static folders name should be good, unless you have a specific reason.

What is a 304 status code and how to fix it?

The 304 status code can be due to a problem on either the server-side or the client-side, so it might take some troubleshooting in order to diagnose and resolve it. The methods you can use to resolve an HTTP 304 status code vary from simple to fairly technical.

How to pass 404 error as parameter in flask?

Flask allows us to make a python file to define all routes and functions. In app.py we have defined the route to the main page (‘/’) and error handler function which is a flask function and we passed 404 error as a parameter.

Why is my website sending an HTTP 304 response?

If the browser copy is outdated, meaning that the file has been modified since the last request, it sends an HTTP 200 code and a new copy is used. Unfortunately, there are a few issues that might cause an HTTP 304 response when it’s not supposed to occur.


2 Answers

In Flask this is very common, whether you use Internal or external files:

HTTP Response

304 NOT MODIFIED

A conditional GET or HEAD request has been received and would have resulted in a 200 OK response if it were not for the fact that the condition evaluated to false.

In other words, there is no need for the server to transfer a representation of the target resource because the request indicates that the client, which made the request conditional, already has a valid representation; the server is therefore redirecting the client to make use of that stored representation as if it were the payload of a 200 OK response.

This means Flask is telling the browser that it already has the content.

If you clear your browser cache and you will notice that Flask returns a 200 on your next request.

like image 179
Mahendra S. Chouhan Avatar answered Oct 27 '22 01:10

Mahendra S. Chouhan


Disabling cash solutions for this issue is not working for me. If it doesn't update the file because it already represented, it means if i have new code in my file, i will never be able to push these modifications

like image 41
SystemX Avatar answered Oct 26 '22 23:10

SystemX