Friday, January 17, 2014

Re: Adding an API to an existing Django app

Hi Dennis,

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

On Friday, January 10, 2014 12:27:26 PM UTC+2, Dennis Plöger wrote:
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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/888bedbe-fb08-4150-a6d9-cbe13cada142%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate