This example will tell you how to add HTTP response headers in Django source code. This example is based on Django Bootstrap3 Example, but you can also create a new Django project to run this example.
1. Add HTTP Response Headers In Django View Function.
- The HTTP response header is always added in the Django project view function before return the HttpResponse object to the client.
- So edit the DjangoHelloWorld / dept_emp / views.py file home_page function as below.
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
2. Verify The HTTP Response Header & Values Via Google Chrome Developer Tools.
- Start the Django project application.
- Browse the web url http://127.0.0.1:8000/dept_emp/ in google chrome.
- Right-click the page, click Inspect menu item in the popup menu list.
- Click the Network tab in the chrome inspector window.
- Select a web resource in the left panel Name list, click the Headers tab in the right panel.
- Now you can see the added HTTP response headers Cache-Control: public,max-age=100000, and Vary:Accept-Encoding.
thanks. it works