Adding an API to an existing Django app

120 views
Skip to first unread message

Dennis Plöger

unread,
Jan 10, 2014, 5:27:26 AM1/10/14
to django...@googlegroups.com
Hi people!

I've just searched some time for a way to acomplish something, that seems to be missing from Django or where there doesn't seem to be a Django app for that. Or maybe I just don't get it.

I have created an app, that has a model and uses CBVs and templates to display a nice Web UI (say hello, SKD). 

Now, I'd like to add a restful API to my app. Basically, I'd like the API to do exactly the same things the Web UI does. I even would like to reuse my existing URLs for that! I explicitly don't want to create shadow views for something like Tastypie or something.

I'm thinking of a way of identifying API-requests (for example, simply adding a "?json"-Parameter) and reacting to this requests.

For example: 

I have a view called UserListView, which reacts to the url "(...)/users/list", is based on a slightly modified version of ListView and displays a nice Web UI listing all users in the system.  If I call the URL like "(...)/users/list?json" I would like to use the exact class, but want to return JSON code returning the same data. (I'm speaking DRY here)

Another example: 

Another view's called UserCreateView, that returns a form when the user GETs the URL "(...)/users/create" and ultimatively creates the user when he POSTs to the URL. Same thing here: When GETting the URL with some magic JSON parameter, I'd like to get JSON code, that describes the needed fields (although this functionality would be optional). When I POST to it with the JSON-parameter, it would accept JSON input, create the user and return some status, also in JSON-form.

Oh, and by the way, I would also like to have my authentication and authorization-checks to work well with the API.

The existing frameworks (like the mentioned Tastypie) only helps creating REST APIs beside the actual code used for WEB-UIs, not reusing it and thus doesn't comply to the DRY-principle in my eyes.

Does somebody know of a library or application, that does what I want or am I getting something wrong here?

Thanks.

Kind regards

Dennis

Dennis Plöger

unread,
Jan 16, 2014, 6:28:59 AM1/16/14
to django...@googlegroups.com
Hi!

It's me again.

I think, I got the whole thing wrong. Instead of plunging an API to an existing WebApp, it would be best to base the WebApp onto the API in the first place. I saw, that you can actually do that using the common Rest frameworks available, so I'm digging my way through that for now.

If anybody has other interesting ideas, please tell.

Kind regards

Dennis

Konstantinos Pachnis

unread,
Jan 17, 2014, 3:14:42 AM1/17/14
to django...@googlegroups.com
Hi Dennis,

You could use content negotiation to achieve what you want http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html

Dennis Plöger

unread,
Jan 21, 2014, 9:54:57 AM1/21/14
to django...@googlegroups.com
Hi!

You could use content negotiation to achieve what you want http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html

No, that's not what I want. I'm actually looking for a way to lift my REST api to the same level as my Web UI.

I have a completely working Web UI, that uses models or querysets to create, update, delete and list objects. Now, for a REST api I would create a shadow structure where I release resources, that are based on the same models or querysets I use in the Web UI.

But I don't want to split these two. If I develop a new feature for the Web UI, it should be in the REST api as well. I want the REST api to be a first class citizen like the Web UI.

Is there a way for the Web UI (which uses generic class based views) to rely on the REST api instead of the ORM? Is this possible with tastypie or whatever?

Do you guys all separate the Web UI from the REST api? Or are you creating full Ajax-Web UIs, that are based on REST apis?

Kind regards
Dennis
Reply all
Reply to author
Forward
0 new messages