Adding REST to Django [closed] Adding REST to Django [closed] python python

Adding REST to Django [closed]


I'm thinking of falling back to simply writing view functions in Django that return JSON results.

  • Explicit
  • Portable to other frameworks
  • Doesn't require patching Django


Please note that REST does not just mean JSON results. REST essentially means exposing a resource-oriented API over native but full-fledged HTTP. I am not an expert on REST, but here are a few of the things Rails is doing.

  • URLs should be good, simple names for resources
  • Use the right HTTP methods
    • HEAD, GET, POST, PUT, and DELETE
    • Optionally with an override (form parameter '_method' will override HTTP request-method)
  • Support content-type negotiation via Accept request-header
    • Optionally with an override (filename extension in the URL will override MIME-type in the Accept request-header)
    • Available content types should include XML, XHTML, HTML, JSON, YAML, and many others as appropriate

For example, to get the native HTTP support going, the server should respond to

GET /account/profile HTTP/1.1Host: example.comAccept: application/json

as it would respond to

GET /account/profile.json HTTP/1.1Host: example.com

And it should respond to

PUT /account/profile HTTP/1.1Host: example.comvar=value

as it would respond to

POST /account/profile HTTP/1.1Host: example.com_method=PUT&var=value


For anyone else looking for a very decent, pluggable API application for Django, make sure you checkout jespern's django-piston which is used internally at BitBucket.

It's well maintained, has a great following and some cool forks which do things like add support for pagination and other authentication methods (OAuth is supported out of the box).

Updated to reflect that django-piston is no longer maintained.