Adding REST to Django [closed]
I've got a Django application that works nicely. I'm adding REST services. I'm looking for some additional input on my REST strategy.
Here are some examples of things I'm wringing my hands over.
- Right now, I'm using the Django-REST API with a pile of patches.
- I'm thinking of falling back to simply writing view functions in Django that return JSON results.
- I can also see filtering the REST requests in Apache and routing them to a separate, non-Django server instance.
Please nominate one approach per answer so we can vote them up or down.
Solution 1:
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
Solution 2:
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.1
Host: example.com
Accept: application/json
as it would respond to
GET /account/profile.json HTTP/1.1
Host: example.com
And it should respond to
PUT /account/profile HTTP/1.1
Host: example.com
var=value
as it would respond to
POST /account/profile HTTP/1.1
Host: example.com
_method=PUT&var=value
Solution 3:
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.