Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 882
S.Lott Avatar asked Nov 21 '08 12:11

S.Lott


People also ask

Can I add Django REST Framework to existing project?

Django Rest Framework is a powerful library that sits on top of existing Django projects to add robust web APIs. If you have an existing Django project with only models and a database--no views, urls, or templates required--you can quickly transform it into a RESTful API with a minimal amount of code.

Is Django GOOD FOR REST API?

Django REST framework (DRF) is a powerful and flexible toolkit for building Web APIs. Its main benefit is that it makes serialization much easier. Django REST framework is based on Django's class-based views, so it's an excellent option if you're familiar with Django.

Can I use Django and Django REST Framework together?

Django Rest Framework makes it easy to use your Django Server as an REST API. REST stands for "representational state transfer" and API stands for application programming interface. Note that with DRF you easily have list and create views as well as authentication.


2 Answers

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
like image 189
Ali Afshar Avatar answered Sep 19 '22 08:09

Ali Afshar


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 
like image 27
yfeldblum Avatar answered Sep 19 '22 08:09

yfeldblum