59
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
30
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
- [Django]-What's the idiomatic Python equivalent to Django's 'regroup' template tag?
- [Django]-How do I remove Label text in Django generated form?
- [Django]-How do I run tests for all my Django apps only?
25
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.
- [Django]-Command not found: django-admin.py
- [Django]-How do I create multiple model instances with Django Rest Framework?
- [Django]-Django โ how to specify a database for a model?
7
Tastypie is also a newly emerging REST framework for Django.
It has the same mindset as pistons, and removes a lot of boilerplate coding.
- [Django]-Django testing: Test the initial value of a form field
- [Django]-Can I make an admin field not required in Django without creating a form?
- [Django]-Django: using <select multiple> and POST
5
My answer to the same question here: Framework for Implementing REST web service in Django
The short version is, have a look at https://github.com/jgorset/django-respite/ a REST framework in its early days, but we use it every day on client projects.
- [Django]-How to expire session due to inactivity in Django?
- [Django]-Django Framework โ Is there a shutdown event that can be subscribed to?
- [Django]-Django 1.4 โ can't compare offset-naive and offset-aware datetimes
4
Scrap the Django REST api and come up with your own open source project that others can contribute to. I would be willing to contribute. I have some code that is based on the forms api to do REST.
- [Django]-How do I get all the variables defined in a Django template?
- [Django]-Django: Get an object form the DB, or 'None' if nothing matches
- [Django]-Django template can't see CSS files
3
Iโm thinking of falling back to simply
writing view functions in Django that
return JSON results.
I would go with that ..
Ali A summed it pretty well.
The main point for me is beign explicit. I would avoid using a function that automatically converts an object into json, what if the object has a reference to a user and somehow the password (even if itโs hashed) go into the json snippit?
- [Django]-How to use the 'reverse' of a Django ManyToMany relationship?
- [Django]-Django โ How to do tuple unpacking in a template 'for' loop
- [Django]-What is the advantage of Class-Based views?
2
I ended up going with my own REST API framework for Django (that Iโd love to get rid of if I can find a workable alternative), with a few custom views thrown in for corner cases I didnโt want to deal with. Itโs worked out ok.
So a combination of 1 and 2; without some form of framework youโll end up writing the same boilerplate for the common cases.
Iโve also done a few stand-alone APIs. I like having them as stand-alone services, but the very fact that they stand alone from the rest of the code leads to them getting neglected. No technical reason; simply out-of-sight, out-of-mind.
What Iโd really like to see is an approach that unifies Django forms and REST APIs, as they often share a lot of logic. Conceptually if your app exposes something in HTML it likely wants to expose it programmatically as well.
- [Django]-Django template system, calling a function inside a model
- [Django]-What's the difference between staff, admin, superuser in django?
- [Django]-Django Count() in multiple annotations
2
You could take look at django-dynamicresponse, which is a lightweight framework for adding REST API with JSON to your Django applications.
It requires minimal changes to add API support to existing Django apps, and makes it straight-forward to build-in API from the start in new projects.
Basically, it includes middleware support for parsing JSON into request.POST, in addition to serializing the returned context to JSON or rendering a template/redirecting conditionally based on the request type.
- [Django]-User Authentication in Django Rest Framework + Angular.js web app
- [Django]-OSError: [Errno 18] Invalid cross-device link
- [Django]-How can i test for an empty queryset in Django?
1
you could try making a generic functions that process the data (like parand mentioned) which you can call from the views that generate the web pages, as well as those that generate the json/xml/whatever
- [Django]-Django 1.8 Run a specific migration
- [Django]-Django ManyToMany filter()
- [Django]-Do I need Nginx with Gunicorn if I am not serving any static content?
- [Django]-Django Model Fields Indexing
- [Django]-How to unit test file upload in django
- [Django]-Django: 'current_tags' is not a valid tag library