Disabling caching in Flask
OK,
finally it worked with this:
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
If you add this, this function will called after each request done. Please,see hereI would be happy, if anyone could explain me why this headers overwriting did not work from the page handler?
Thank you.
How to prevent cached response (flask server, using chrome)
To prevent cached responses from the browser:
- press F12 or right click > inspect
- click network tab
- check "Disable cache".
To prevent cached responses from the server: add the following definition to application.py:
# prevent cached responses
if app.config["DEBUG"]:
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
Disable cache on a specific page using Flask
You can try adding cache control headers only if there are no such headers for a specific page:
@app.after_request
def add_header(response):
response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
if ('Cache-Control' not in response.headers):
response.headers['Cache-Control'] = 'public, max-age=600'
return response
And in your page code - e.g.:@app.route('/page_without_cache')
def page_without_cache():
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
return 'hello'
The point is that you shouldn't override your headers in @app.after_request
for all pages - only for those where cache isn't turned off explicitly.Further, you might want to move the code adding headers to a wrapper such as @no_cache
- so you can use it just like that:
@app.route('/page_without_cache')
@no_cache
def page_without_cache():
return 'hello'
How to disable Flask-Cache caching
Simply set your app.config's CACHE_TYPE
key to "null"
before you initialize Flask-Cache:
app.config["CACHE_TYPE"] = "null"
# change to "redis" and restart to cache again
# some time later
cache.init_app(app)
# All caching functions will simply call through
# to the wrapped function, with no caching
# (since NullCache does not cache).
How to run Flask without caching templates?
You need to enable the debug mode in your app config: app.debug = True
or app.config['DEBUG'] = True
. Just make sure you enable this flag only in development since it can have performance and security implications. You can find more details in Flask documentation.
I've tried all the common ways of disabling css caching in Flask, and none of them work for me
I tried troubleshooting by making a minimal flask project, and did not run into the same issue. Then I tried my original project again, and the issue as magically fixed. Not sure why, but I'll take it. I appreciate this means this question is not useful to anyone else, sorry about that.
python - how to disable cache in Flask? Getting 500 server error during second POST request sent to server while first request was 200 OK
this is not a caching problem, it's a problem in the logic of the function,
on the first call, you probably don't get to the line of code generating the exception,
The problem is on the second call, the key name is not 'ws' it's 'ws_no',
if you change these lines:
for i in repository:
if i['ws'] == received_data['ws_no']:
i['part'] = received_data['part_no']
updated = 'yes'
to use 'ws_no', and 'part_no' you won't have the key error. Force cache reload on Flask
By default, Flask doesn't use cache in such ways, it caches only static data if not specified. If a page has changed, flask will force the browser to reload new data instead of using cache. Any restart can kill all the cache used earlier, meaning that all users previously accessed the page will lost all their cache.
Related Topics
Networkx - Change Color/Width According to Edge Attributes - Inconsistent Result
Source Interface with Python and Urllib2
How to Import a Module in Python with Importlib.Import_Module
Multiple Plots in One Figure in Python
Python & Pandas: How to Query If a List-Type Column Contains Something
Syntax Error: Invalid Syntax' for No Apparent Reason
Parallelize Apply After Pandas Groupby
Use Index in Pandas to Plot Data
How to Skip Iterations in a Loop
Row-Wise Average for a Subset of Columns with Missing Values
Comparable Classes in Python 3
How to Plot Only a Table in Matplotlib
Pandas: Subindexing Dataframes: Copies VS Views