How to apply decorator to every Flask view How to apply decorator to every Flask view flask flask

How to apply decorator to every Flask view


To add headers to every outgoing response, use the @Flask.after_request hook instead:

@app.after_requestdef add_ua_compat(response):    response.headers['X-UA-Compatible'] = 'IE=Edge'    return response

There is a Flask extension that does exactly this; register the hook and add a header.


You might consider just writing a custom WSGI middleware. You can snag all of your application's responses and augment the headers as necessary. The quickstart discusses how to hook in a middleware and there are no shortage of WSGI tutorials on how to add headers to the start_response