How do you get Django to make a RESTful call?

2019-02-01 00:13发布

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.

标签: django http rest
3条回答
Animai°情兽
2楼-- · 2019-02-01 00:54

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

查看更多
够拽才男人
3楼-- · 2019-02-01 00:59

“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).

查看更多
疯言疯语
4楼-- · 2019-02-01 01:08

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

查看更多
登录 后发表回答