Hi everyone, i'm new to python and web development. I worked through the Quickstart and Tutorial and begun working on a small project. For this project i want to explicity set 'no-cache' on the response for a particular view. I'm using render_template() to render a template. Googeling and searching Stack Overflow doesn't yield anything useful. Can someone please post an example how to do this? Thanks in advance, Karsten
Hi,
As said, you can set headers on the response objects. Either by using
make_response to explicitly get one or in a decorator:
Method 1:
@app.route('/nocache')
def something_not_cached():
resp = make_response(render_template(...))
resp.cache_control.no_cache = True
return resp
Or you write a decorator:
@app.route('/nocache')
@nocache
def something_not_cached():
return render_template(...)
And here is the decorator:
from flask import make_response
from functools import update_wrapper
def nocache(f):
def new_func(*args, **kwargs):
resp = make_response(f(*args, **kwargs))
resp.cache_control.no_cache = True
return resp
return update_wrapper(new_func, f)
resp.cache_control is an accessor for the Cache-Control header, you can
also modify the header directly:
resp.headers['Cache-Control'] = 'no-cache'
Regards,
Armin
Hi, wow, thanks for the detailled reply. I implemented the decorator but i should read up on decorators ;) Thanks Karsten Am 09.08.2011 12:26, schrieb Armin Ronacher: > Hi, > > As said, you can set headers on the response objects. Either by using > make_response to explicitly get one or in a decorator: > > Method 1: > > @app.route('/nocache') > def something_not_cached(): > resp = make_response(render_template(...)) > resp.cache_control.no_cache = True > return resp > > Or you write a decorator: > > @app.route('/nocache') > @nocache > def something_not_cached(): > return render_template(...) > > And here is the decorator: > > from flask import make_response > from functools import update_wrapper > > def nocache(f): > def new_func(*args, **kwargs): > resp = make_response(f(*args, **kwargs)) > resp.cache_control.no_cache = True > return resp > return update_wrapper(new_func, f) > > resp.cache_control is an accessor for the Cache-Control header, you can > also modify the header directly: > > resp.headers['Cache-Control'] = 'no-cache' > > > Regards, > Armin
See the documentation for make_response. You want to use make_response to get a Response object and then you can set headers like res.headers['Cache-Control'] = ... On Mon, Aug 8, 2011 at 4:12 PM, Karsten Hoffrath <khoffrath@khoffrath.de>wrote: > Hi everyone, > > i'm new to python and web development. I worked through the Quickstart > and Tutorial and begun working on a small project. > For this project i want to explicity set 'no-cache' on the response for > a particular view. I'm using render_template() to render a template. > > Googeling and searching Stack Overflow doesn't yield anything useful. > > Can someone please post an example how to do this? > > Thanks in advance, > Karsten >
Thanks for your reply, i will give it a try tonight. Karsten Am 09.08.2011 03:16, schrieb Jay Baker: > See the documentation for make_response. > > You want to use make_response to get a Response object and then you can set headers like > > res.headers['Cache-Control'] = ... > > On Mon, Aug 8, 2011 at 4:12 PM, Karsten Hoffrath <khoffrath@khoffrath.de > <mailto:khoffrath@khoffrath.de>> wrote: > > Hi everyone, > > i'm new to python and web development. I worked through the Quickstart > and Tutorial and begun working on a small project. > For this project i want to explicity set 'no-cache' on the response for > a particular view. I'm using render_template() to render a template. > > Googeling and searching Stack Overflow doesn't yield anything useful. > > Can someone please post an example how to do this? > > Thanks in advance, > Karsten > >