如何用Python/Django实现RESTful网络服务
我想搭建一个使用 Django/Python 创建的网络服务的 Ubuntu 服务器,有人能提供一些资源、教程或者示例代码吗?
2 个回答
1
6
还有一个叫做 piston 的工具,它是一个用于创建RESTful API的Django框架。虽然学习起来有点难度,但它和Django结合得很好。
如果你想要更轻量级的东西,Simon Willison有一个非常不错的代码片段,我之前用过,能够很好地模拟HTTP方法:
class ArticleView(RestView):
def GET(request, article_id):
return render_to_response("article.html", {
'article': get_object_or_404(Article, pk = article_id),
})
def POST(request, article_id):
# Example logic only; should be using django.forms instead
article = get_object_or_404(Article, pk = article_id)
article.headline = request.POST['new_headline']
article.body = request.POST['new_body']
article.save()
return HttpResponseRedirect(request.path)
Jacob Kaplan-Moss写了一篇很不错的文章,叫做REST中的最差实践,可以帮助你避免一些常见的错误。