如何让 Django 进行 RESTful 调用?
注意:我并不是想提供 RESTful api,而是想调用一个。
def index( request, *args, **kwargs ):
context = {}
context['some_json'] = make_remote_api_call( "http://exampl.com/objects/" )
return render_to_response( 'index.html', context, context_instance=RequestContext(request) )
或者这只是荒谬的,我应该让客户总是这样做?基本上,我很好奇如何正确划分我的网站,以便不同的服务器(内部受保护的服务器)可以提供不同的信息子集。
Note: I'm not trying to provide a RESTful api, I'm trying to call one.
def index( request, *args, **kwargs ):
context = {}
context['some_json'] = make_remote_api_call( "http://exampl.com/objects/" )
return render_to_response( 'index.html', context, context_instance=RequestContext(request) )
Or is this just ridiculous and I should make the client always do it? Basically, I'm curious as to how to properly divide my website so that different servers (internal protected ones) can provide different subsets of info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 requests 库。 这里有一些讨论,我做了一个关于“使用 Python 使用 Web API”的演讲在 PyOhio,您可能会发现有趣或有帮助
Use the requests library. Here is some discussion on it and I did a talk on "Consuming Web APIs with Python" at PyOhio that you might find interesting or helpful
实际上,“RESTful 调用”仅意味着发出 HTTP 请求。 Python 中有几个内置模块可以让您执行此操作:
然后是第 3 方模块,例如 @Issac提到( ,基于我对 urllib2 和 httplib 的非常有限的经验,对于您的目的来说,看起来比它们好得多)。
“RESTful call” in practice just means making an HTTP request. There are a couple of built-in modules in Python that let you do this:
Then there are 3rd-party modules, like the one mentioned by @Issac (which, based on my very limited experience with urllib2 and httplib, looks much better than them for your purposes).
Slumber 是一个 RESTful 数据连接器,可用于从 Django 系统提供适当的 RESTful 数据服务。
https://github.com/KayEss/django-slumber
Slumber is a RESTful data connector that can be used to make proper RESTful data services from Django systems.
https://github.com/KayEss/django-slumber