开发者

What is an elegant way to create application access URLs for an existing web application?

I've been assigned to create an RESTful Android application for an existing web service which is built using Django.

My current design idea is to have the Android application receive a JSON version of the data that would normally be sent to the Django template on each url. So my view would look like:

#The site stores and organizes user's medical experiences by allowing search of 
# what treatments have been effective for a particular condition
treatment_for开发者_如何学Python_condition = {'treatment' : treatment, 'condition' : condition}
if send_as_json :
     return HttpResponse(json.dumps(treatment_for_condition),mimetype='application/json')
else:
     t = loader.get_template('results.html')
     return HttpResponse(t.render(treatment_for_condition))

Is there an elegant way to set the "send_as_json" variable? I'm considering the two following strategies:

1) Adding a qualifier to the end of all URLs so that /condition/treatment/ will return a webpage and /condition/treatment/?json=true will return a JSON object

2) Creating a subdomain json.treatmentreport.com that will set the "send_as_json" variable to true, then dispatch to the same view as if it were in the www domain.

Can either of these solutions be implemented elegantly? Or am I going about this completely the wrong way?


I read over your question, and I think instead of suggesting methods to fix your existing setup I will try to answer the head of your question first:

"What is an elegant way to create application access URLs for an existing web application?"

Check out django-piston

Piston is a relatively small Django application that lets you create application programming interfaces (API) for your sites.

It has several unique features:

  • Ties into Django's internal mechanisms.
  • Supports OAuth out of the box (as well as Basic/Digest or custom auth.)
  • Doesn't require tying to models, allowing arbitrary resources.
  • Speaks JSON, YAML, Python Pickle & XML (and HATEOAS.) Ships with a convenient reusable library in Python
  • Respects and encourages proper use of HTTP (status codes, ...)
  • Has built in (optional) form validation (via Django), throttling, etc.
  • Supports streaming, with a small memory footprint.
  • Stays out of your way.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜