Sometimes, we want to disable caching in Python Flask.
In this article, we’ll look at how to disable caching in Python Flask.
How to disable caching in Python Flask?
To disable caching in Python Flask, we can set the response headers to disable cache.
For instance, we write
@app.after_request
def add_header(r):
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
to create the add_header function that adds a few headers to the response after each request is done.
We make it run after each request with the @app.after_request decorator.
And then we add the Expires and Cache-Control headers and set their values all to 0 to disable caching.
Conclusion
To disable caching in Python Flask, we can set the response headers to disable cache.