Whereas in Django, by default you have to check for the specific
HTTP_ACCEPT header you want to work with, in Rails, you don’t. This is known as Content Negotiation.
Content Negotiation in Rails
So rather than having an
if statement checking for the specific accept header, you just use
respond_with. In Rails 2.X, you would do something like so:
respond_to block. In it, you just tell Rails which format to use when expecting a certain format.
In Rails 3, it’s even easier. Using
respond_with, you can now do:
You can read more details on how to use
respond_with on Ryan Daigle’s blog and about ActiveModel on the Engine Yard blog. In detail, Yehuda explains that the “available providable formats are transparently determined by introspecting the object”. To me, this is great as it’s one less
import statement to worry about.
Content Negotiation in Django
James Bennett wrote an excellent article on doing content negotiation in Django here. It is based on Daniel Lindsley’s work here, which was “loosely based around similar functionality, called
respond_to in Rails”. I haven’t tried the code in the articles yet, but the idea looks very good and will probably provide me with what I want in Django without having my code splattered with
if statements for the sake of content negotiation.
Questions? E-mail me: this domain AT gmail DOT com