Monday, March 15, 2010

Django HTTP Response

The three main elements of an HTTP response are the body, the headers, and possibly cookie values. The Django HttpResponse object has support for all three, which is a requirement considering Django is a web application framework.

Whats cool about this Django class is how easy it is to test if a header exists. This is done by implementing the __contains__ method. It seems trivial when building classes, to add something like this in later, but it is easy to just forget about it.

It makes interacting with HttpResponse instances that much easier because there is one less step. Instead of doing if "key" in response_instance.headers().keys(), or something like that, Django HttpResponse instance allow developers to do if "key" in response_instance. The reason this functionality is implemented for headers and not for cookies is because headers are used more frequently than cookies in mos circumstances.