Need some advise on how to use a RESTful API

39 views
Skip to first unread message

Mario Osorio

unread,
Dec 20, 2013, 11:45:45 AM12/20/13
to django...@googlegroups.com
I need to work on a django app that will eventually benefit from a RESTful API so it can be used from any web browser as well a from apps writtens for iOS and Android.

Being so new to django development I don't even have any ideas on how to use a RESTful API, for example,

  1. Should I begin the development use a RESTful API, or should I add ir when needed? ( it IS going to be used anyways)
  2. Of course I understand I'm adding another layer of work to the final product but, can someone please explain how bad deep and how complicated this layer might be?
  3. I've found a couple of online tutorials and I'll soon be working on them, but I honestly think there is nothing quite like reading the opinions of different people as to what the workflow with a RESTful API in django implies. I think a simple overview of the general workflow will answer most of the questions I have in mind right now.

Thanks a lot in advanced!

Doug Blank

unread,
Dec 20, 2013, 4:11:59 PM12/20/13
to django-users
I'm not an expert, but I just happen to be working on something similar.

I already had the URLs set up so that the HTML was being rendered,
edited, saved, etc. So, then I wanted to add a RESTful API, mirroring
the same URLs. A regular URL looks like:

http://localhost:8000/person/764526236

I decided to make the REST API look like:

http://localhost:8000/person/764526236/?format=json

That was as easy as something like this (person is the view, 764526236
is the handle):

try:
obj = Person.objects.get(handle=handle)
except:
raise Http404(_("Requested %s does not exist.") % view)
if not check_access(request, context, obj, act):
raise Http404(_("Requested %s does not exist.") % view)
if "format" in request.GET:
if request.GET["format"] == "json":
person = db.get_person_from_handle(obj.handle)
content = str(person.to_struct())
response = HttpResponse(content, mimetype="application/json")
return response

The 'if "format"' handles the different formats.

Hope that helps (or someone can point us both in a better direction).

-Doug

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/cc10edc2-e978-461e-986f-43dd73eea63e%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Anders Petersson

unread,
Dec 20, 2013, 6:06:58 PM12/20/13
to django...@googlegroups.com
I usually write the web application first, without consuming my own API, and when I need to build versions for other formats, such as mobile native apps, I create an API and consume this from the App.

There are several framework to make creating an API for you application easier, popular examples are Tastypie [1] and django-rest-framework [2].

There are alot of buzz about building a service infrastructure for apps, but really, you shouldnt start there if you're new. If you're using class-based-views, subclassing them and generating JSON instead will be very easy. 

Nick Apostolakis

unread,
Dec 20, 2013, 11:23:48 PM12/20/13
to django...@googlegroups.com

Hello there,
In my point of view, when you develop an app using a rest full api you go through the same moves as when writing the web app using the native interface.
In my case I have created both. I wrote first the web interface and then the api, but a different approach would be to write first the api and then use a restful capable framework in order to create the web interface if needed, along with the mobile restful capable clients. It depends on your workflow.

--
Reply all
Reply to author
Forward
0 new messages