Sending HTTP Headers with Django

Sending HTTP headers is very easy, but not necessarily obvious. When your view returns the content of a page if you have followed the tutorials you will be writing something like:


return HttpResponse(t.render(c))

HttpResponse objects contain a headers dictionary, that you can easily add and remove headers from using dictionary notation eg:


response = HttpResponse(t.render(c))
response['Content-Type'] = 'application/atom+xml'
return response

Comments

Matt Croydon::Postneo 2.0 » Blog Archive » HTTP Headers and Django

[...] Andrew Brehaut has a hot tip about HTTP headers and Django: HttpResponse objects contain a headers dictionary, that you can easily add and remove headers from using dictionary notation [...]

Adrian Holovaty

Here's a shorter way of accomplishing the same thing as in your above example:

return HttpResponse(t.render(c), mimetype='application/atom+xml')