>>> request.headers
{'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6', ...}
>>> 'User-Agent' in request.headers
True
>>> 'user-agent' in request.headers
True
>>> request.headers['User-Agent']
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
>>> request.headers['user-agent']
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
>>> request.headers.get('User-Agent')
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
>>> request.headers.get('user-agent')
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
from django.http import HttpResponse
def index(request):
response = HttpResponse("Hello world!")
response["My-Header"] = "header value"
return response
def home_page(request):
# get the django.http.response.HttpResponse object
resp = render(request, 'dept_emp/home_page.html')
# set http response header and value.
resp['Cache-Control'] = 'public,max-age=100000'
resp['Vary'] = 'Accept-Encoding'
# return the HttpResponse object.
return resp
class MyMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
response['X-My-Header'] = "my value"
return response