How to add an HTTP header to all Django responses

前端 未结 2 2010
野性不改
野性不改 2020-12-30 20:41

I\'d like to add a few headers to all responses that my Django website returns. Is there a way to do this (besides adding a wrapper to the render function)?

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 20:59

    When returning JsonResponse.

    from django.http import JsonResponse
    
    data = {'key','value'} # some data
    
    response = JsonResponse(data,status=200)
    
    response['Retry-after'] = 345 # seconds 
    response['custom-header'] = 'some value'
    
    return response 
    

提交回复
热议问题