python 在 Django 中实现 REST web 服务的框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2488325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Framework for Implementing REST web service in Django
提问by Laizer
I'm looking to implement a RESTful interface for a Django application. It is primarily a data-service application - the interface will be (at this point) read-only.
我正在寻找为 Django 应用程序实现 RESTful 接口。它主要是一个数据服务应用程序 - 接口(此时)是只读的。
The question is which Django toolsets / frameworks make the most sense for this task.
问题是哪些 Django 工具集/框架最适合这项任务。
I see Django-rest and Django-piston. There's also the option of rolling my own.
我看到了 Django-rest 和 Django-piston。也可以选择自己滚动。
The question was asked here, but a good two years back. I'd like to know what the current state of play is.
这个问题是在这里问的,但是两年前的事了。我想知道目前的比赛状态。
In this question, circa 2008, the strong majority vote was to not use any framework at all - just create Django views that reply with e.g. JSON. (The question was also addressed, crica 2008, here.)
在这个问题中,大约在 2008 年,绝大多数投票是根本不使用任何框架 - 只需创建使用例如 JSON 回复的 Django 视图。(这个问题也得到了解决,crica 2008,这里。)
In the current landscape, what makes the most sense?
在当前的形势下,什么最有意义?
采纳答案by Daniel Naab
NOTE: Since this post was written,
django-piston
is no longer actively maintained. As others have mentioned, look intotastypie
ordjango-rest-framework
.
注意:自从写了这篇文章,
django-piston
不再积极维护。正如其他人所提到的,请查看tastypie
或django-rest-framework
。
Indeed, you can roll your own, but there's a lot of boilerplate involved.
确实,您可以推出自己的,但涉及很多样板。
django-pistonis an exceptionally easy to use, and extensible, micro-framework. In addition to mocking up all the necessary views and url patterns, it supports directly mapping models to a REST interface, which is nice if you have a simple use case. I'd suggest looking into it.
django-piston是一个非常易于使用且可扩展的微型框架。除了模拟所有必要的视图和 url 模式之外,它还支持将模型直接映射到 REST 接口,如果您有一个简单的用例,这很好。我建议调查一下。
回答by ncoghlan
And since this question still rated pretty highly in my searches on Google, I'll add this alternative to the mix: http://django-rest-framework.org/
由于这个问题在我在 Google 上的搜索中仍然得到了很高的评价,我将这个替代方案添加到组合中:http: //django-rest-framework.org/
My initial impression is that it does a very good job of embodying the RESTful API design principles described here: http://readthedocs.org/docs/restful-api-design/en/latest/
我的初步印象是它很好地体现了此处描述的 RESTful API 设计原则:http: //readthedocs.org/docs/restful-api-design/en/latest/
回答by espenhogbakk
One way is to roll your own, or use django-piston which is excellent. But the problem I have with piston is that is kind a made to be attached to a existing django-project to add an API. It is not so much made for building a resource oriented API with support for formats including HTML.
一种方法是滚动你自己的,或者使用 django-piston,这是非常好的。但我对活塞的问题是,它是一种附加到现有 django 项目以添加 API 的问题。它并不是用来构建面向资源的 API 并支持包括 HTML 在内的格式。
The way I see the use case for Piston is, you have a complete website that serves up html content, but then you would attach an api to that at the url /api/*. Then you go and add Piston to it. With this use case Piston is great, no rewrite needed for existing code and you get whatever you need. It may be that Piston works well without separating the api from the user facing part of the site, but I haven't tried that.
我看到 Piston 用例的方式是,您有一个提供 html 内容的完整网站,但随后您将在 url /api/* 上附加一个 api。然后你去添加活塞。有了这个用例,Piston 很棒,现有代码无需重写,您可以获得所需的任何内容。可能是 Piston 在没有将 api 与网站面向用户的部分分开的情况下运行良好,但我还没有尝试过。
It is easier to try to explain this with some examples:
用一些例子来解释这个更容易:
Bitbucket
比特桶
- User facing url and content: https://bitbucket.org/jespern
- Api url and content: https://api.bitbucket.org/1.0/users/jespern/
- 面向用户的网址和内容:https: //bitbucket.org/jespern
- Api 网址和内容:https: //api.bitbucket.org/1.0/users/jespern/
Github
GitHub
- User facing url and content: https://github.com/jgorset
- Api url and content: https://github.com/jgorset.json
- 面向用户的网址和内容:https: //github.com/jgorset
- api url和内容:https: //github.com/jgorset.json
Bitbucket is made with Django and django-piston for the API, while Github is made with Ruby On Rails wich has built in support for resources with different formats. I am not saying that you should switch to RoR, because we all love Django, and I am not saying that you couldn't do this with django, but it would be tedious to do this yourself on every view.
Bitbucket 是用 Django 和 django-piston 制作的 API,而 Github 是用 Ruby On Rails 制作的,它内置了对不同格式资源的支持。我并不是说你应该切换到 RoR,因为我们都喜欢 Django,我并不是说你不能用 django 做到这一点,但在每个视图上自己做这件事会很乏味。
So a co-worker and I decided that we wanted some of the "magic" of RoR in Django, but we didn't want it to be magical, but make our lives easier without abstracting everything. So jgorsetwrote Respitewhich is a little Django framework to make "RESTful" APIs in the same fashion as RoR does, but in Django. It is still in early development, but we use it daily in our work projects and it is highly customizable, much like Django itself. It tries to simplify making resource oriented webpages, and structure your code without getting in your way.
所以我和一个同事决定我们想要在 Django 中使用 RoR 的一些“魔法”,但我们不希望它变得神奇,而是让我们的生活更轻松,而不是抽象一切。所以jgorset写道喘息这是一个小Django框架,使以同样的方式“REST风格的” API,作为回报率的做法,但在Django。它仍处于早期开发阶段,但我们每天都在我们的工作项目中使用它,并且它是高度可定制的,就像 Django 本身一样。它试图简化面向资源的网页的制作,并在不妨碍您的情况下构建您的代码。
So naturally my recommendation would be to look at, and try Respite: https://github.com/jgorset/django-respite/
所以很自然地,我的建议是查看并尝试 Respite:https: //github.com/jgorset/django-respite/
回答by S.Lott
Still true.
尚真。
It's quite trivial to roll your own. Each REST URI maps to a view function. Each REST method (GET, POST, PUT, DELETE) is a simple condition in the view function.
推出自己的产品非常简单。每个 REST URI 都映射到一个视图函数。每个 REST 方法(GET、POST、PUT、DELETE)都是视图函数中的一个简单条件。
Done.
完毕。
回答by Alex Ciminian
tastypieis also an option, I've just tried it and it seemed painless until now. I'm playing with a dummy app that exposes an API to a backbone.js client and I've hit no brick walls with this library. This articlegot me to try it.
美味派也是一种选择,我刚刚尝试过,直到现在它似乎都没有痛苦。我正在使用一个向backbone.js 客户端公开API 的虚拟应用程序,并且我没有使用这个库碰到任何砖墙。这篇文章让我尝试了一下。