【发布时间】:2011-03-14 14:38:45
【问题描述】:
我被分配为使用 Django 构建的现有 Web 服务创建一个 RESTful Android 应用程序。
我当前的设计理念是让 Android 应用程序接收一个 JSON 版本的数据,这些数据通常会在每个 url 上发送到 Django 模板。所以我的看法是这样的:
#The site stores and organizes user's medical experiences by allowing search of
# what treatments have been effective for a particular condition
treatment_for_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))
有设置“send_as_json”变量的优雅方法吗?我正在考虑以下两种策略:
1) 在所有 URL 的末尾添加一个限定符,以便 /condition/treatment/ 将返回一个网页 和 /condition/treatment/?json=true 将返回一个 JSON 对象
2) 创建一个子域 json.treatmentreport.com,它将“send_as_json”变量设置为 true,然后分派到与 www 域中相同的视图。
这些解决方案中的任何一个都可以优雅地实施吗?还是我完全走错了路?
【问题讨论】: