Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: how to add http header to responses in a convenient way?

I know we can use HttpResponseInstance['headername'] = 'headervalue' to add header to http response. But in this way, I have to rewrite all the generic view I am using, which makes much more work.

Is there a convenient way to add header to responses, like a callback for responses, or a url decorator?

like image 228
dspjm Avatar asked Feb 08 '23 03:02

dspjm


2 Answers

You can write a middleware class and implement the method process_response.

like image 90
Eugene Primako Avatar answered Feb 12 '23 12:02

Eugene Primako


You should use a middleware class. Check out this thread.

Django 1.10 and above __init__ and __call__ should be implement. process_response is deprecated.

like image 31
Józsa Csongor Avatar answered Feb 12 '23 11:02

Józsa Csongor