Adding REST to Django

I have a Django application that works great. I am adding REST services. I am looking for additional input to my REST strategy.

Here are some examples of what I squeeze with my hands.

  • I am currently using the Django-REST API with a bunch of patches.
  • I am thinking about reverting to writing simple Django view functions that return JSON results.
  • I also see filtering REST requests in Apache and routing them to a separate server instance other than Django.

Please assign one approach for each answer so that we can vote for them up or down.

+48
python rest django apache
Nov 21 '08 at 12:24
source share
11 answers

I am thinking of returning to a simple record of view functions in Django, which returns JSON results.

  • Explicit
  • Portable for other frameworks
  • Doesn't require Django fix
+57
Nov 21 '08 at 13:38
source share

Note that REST does not mean only JSON results. REST essentially means providing a resource-oriented API on top of native but fully functional HTTP. I'm not a REST specialist, but here are some of the things Rails does.

  • URLs should be good, simple names for resources
  • Use the correct HTTP methods
    • HEAD, GET, POST, PUT and DELETE
    • Optional with override (form parameter '_method' overrides HTTP request-method)
  • Support for content type negotiation using the Accept-header header
    • Optional with an override (the file name extension in the URL overrides the MIME type in the Accept request header)
    • Available content types should include XML, XHTML, HTML, JSON, YAML, and many others, if necessary.

For example, to receive native HTTP support, the server must respond to

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

as he would answer

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

And he must answer

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

as he would answer

 POST /account/profile HTTP/1.1 Host: example.com _method=PUT&var=value 
+30
Oct 02 '09 at 15:03
source share

For anyone looking for a very decent, plugin API application for Django, make sure you check out the jespern django-piston that is used inside BitBucket.

It is well supported, has great footprints and some cool forks that do things like adding pagination support and other authentication methods (OAuth is supported out of the box).

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

+25
Mar 29
source share

Tastypie is also the new REST framework for Django. It has the same thinking as pistons and removes a lot of coding.

+7
Dec 05 2018-11-12T00:
source share

My answer to the same question: Framework for implementing REST web service in Django

Short version: take a look at the https://github.com/jgorset/django-respite/ REST structure in its early days, but we use it every day on client projects.

+5
Jul 06 2018-11-11T00:
source share

Drop the Django REST api and come up with your own open source project that others can contribute. I would like to contribute. I have code based on api forms to do REST.

+4
Nov 21 '08 at 14:07
source share

I am thinking of returning to a simple record of view functions in Django, which returns JSON results.

I would go with this ..
Ali A very well indicated.

The main thing for me is the obvious box. I would avoid using a function that automatically converts the object to json, what if the object has a link to the user and somehow the password (even if it is hashed) goes into json snippit?

+3
Nov 23 '08 at 14:01
source share

I ended up working with my own REST API base for Django (which I would like to get rid of if I could find a workable alternative), with a few user views that were selected for corner cases that I did not want to deal with. Everything turned out fine.

So, a combination of 1 and 2; without any form of framework you end up writing the same template for ordinary cases.

I also made some standalone APIs. I like to have them as standalone services, but the fact that they are separate from the rest of the code leads to the fact that they are ignored. No technical reason; just out of sight, out of consciousness.

What I really would like to see is an approach that unifies Django forms and REST APIs, as they often share a lot of logic. Conceptually, if your application exposes something in HTML, it probably also wants to display it programmatically.

+2
Nov 23 '08 at 20:26
source share

You can take a look at django-dynamicresponse , which is an easy basis for adding REST APIs with JSON to your Django applications.

This requires minimal changes to add API support to existing Django applications and allows you to directly embed the API from the very beginning in new projects.

It basically includes middleware support for parsing JSON in request.POST, in addition to serializing the returned JSON context or rendering the template / redirect conditionally based on the type of request.

+2
Oct 21 2018-10-10
source share

you can try to create general functions that process data (for example, specified in the parade) that you can call from views generating web pages, as well as those that generate json / xml / whatever

+1
Apr 10 '09 at 1:09
source share

TastyPie looks pretty interesting and promising. It goes well with Django.

+1
Jan 27 '12 at 1:17
source share



All Articles