How do you use Django forms when receiving JSON Ajax requests from an iPad?
I'm sending a lot of JSON requests from a native iPad application to my Django web server. How do I translate forms I use on my website to handle an iPad web service?
Am I trying to solve the wrong problem, sho开发者_JAVA技巧uld a web service used from native iPad applications be redesigned to use REST-ful requests?
Well, first of all, this question should really be:
"How do I write a RESTful API using Django and JSON?"
iPads are just like any other web browser (client), and they can use javascript, JSON, etc.
Here's a high level description of what you need to do:
- Write a Django view and map it to a URL, eg: /api/some_action/
- Write out the body of your view, have it perform whatever action you need on the server.
- Write the HTML/javascript code which is displayed on a user's iPad, so that when iPad users visit a part of your website (let's say /home/) they'll make a JSON request to your server which talks to the API (eg, sends some JSON to /api/some_action/)
Once your Javascript code sends JSON to the API view, your view should process that JSON, and perform whatever actions you want.
This is the way most web-services are developed.
Hope that helps!
Can the iPad (or iPhone/iPod) browser send PUT/DELETE commands? For me that's the biggest trouble when trying to do REST-like apps in JavaScript.
In the end, what i tend to do is to have small Django views (mostly using the create_update
generic views) to handle the HTML/form/model integration; and in JS, i use jQuery's $('#dialog').dialog().load('dialogurl')
to open a dialog and load it with the form generated by Django. Be sure to either manage the submit()
yourself.
I'd prefer a lot to just write a REST server (probably using Django-Piston) and a full client app on the browser; but so far i haven't found a nice enough JS framework. (pyjamas or qooxdoo sound great, but fall 'just a little short')
Django TastyPie address this need - RESTful and Ajax suitable for iOS (iPhone/iPad) and Android Tablets.
http://django-tastypie.readthedocs.org/en/latest/index.html
http://django-tastypie.readthedocs.org/en/latest/tutorial.html#adding-to-the-api
This makes even more data accessible, so if we start up the runserver again, the following URLs should work:
* http://127.0.0.1:8000/api/v1/?format=json
* http://127.0.0.1:8000/api/v1/user/?format=json
* http://127.0.0.1:8000/api/v1/user/1/?format=json
* http://127.0.0.1:8000/api/v1/user/schema/?format=json
* http://127.0.0.1:8000/api/v1/user/set/1;3/?format=json
* http://127.0.0.1:8000/api/v1/entry/?format=json
* http://127.0.0.1:8000/api/v1/entry/1/?format=json
* http://127.0.0.1:8000/api/v1/entry/schema/?format=json
* http://127.0.0.1:8000/api/v1/entry/set/1;3/?format=json
Here's demo https://github.com/natea/Valentunes
It is has a web client iPhone app (search it).
精彩评论