我尝试访问其他服务的API,利用我的模型的领域API请求的关键字。该网址会像像这样:
I'm trying to access another service's API, using my model's fields as the keywords in the API request. The URL would be like like so:
http://api.example.com/json/?first_name=FNAME&last_name=LNAME&key={key}
下面是我的code从views.py:
Here's my code from views.py:
class ExamplePersonView(ListView):
context_object_name = "example_person"
template_name = "templates/example_person.html"
def get_queryset(self):
lname = get_object_or_404(ExamplePeople, lname__iexact=self.args[0])
return ExamplePeople.objects.filter(lname=lname)
据我了解,我需要使用AJAX我的页面模板和我的views.py之间的通信发送请求,然后present页面上的信息。
From what I understand, I'll need to use AJAX to communicate between my page template and my views.py to send the request and then present the information on the page.
我发现,帮助您从其他服务访问API的几个Django的应用程序,可以很容易地把你的模型转换成一个公共的API,但没有。有谁知道这样一个应用程序呢?
I've found several Django apps that make it easy to turn your models into a public API, but none that help you access API's from another service. Does anyone know of an app like that?
如果没有,没有任何人有使用AJAX使用Django发出请求和present它在模板中有很好的理解?
If not, does anyone have a good understanding of using AJAX with Django to make the request and present it in the template?
有多种方式与洋的API进行通信。有没有必要为Ajax。 Ajax是刚刚制作的模板后台调用,从而触发任何事件你的想法。
There's several ways to communicate with a "foreign" API. There's no necessity for ajax. Ajax is just for making background calls in a template, triggering whatever event you have in mind.
但是,假设你想与facebook的GraphAPI通信,以获取配置文件
But let's say you want to communicate with the facebook GraphAPI to retrieve a profile
http://graph.facebook.com/bill.clinton
标准的结果序列化为JSON,这很容易实现到AJAX或任何JavaScript库,故名JavaScript对象符号。
The standard result is serialized as JSON, which implements easily into AJAX or any JavaScript library, hence the name JavaScript Object Notation.
因此,与AJAX的例子可能是:
So an example with AJAX might be:
function callFacebook() {
$.ajax({
type: "GET",
data: ({}),
dataType: 'json',
url: "http://graph.facebook.com/bill.clinton",
success: function(data){
alert("Hi I am former "+data.name);
}
});
}
callFacebook();
在你的JavaScript文件或脚本标记之间的模板中包含这一点,你应该得到一个不错的警报消息:
Include this in your javascript file or within your template between script tags and you should get a nice alert message:
你好,我前president克林顿
Hi I am former President Bill Clinton
现在你可以把这个警报到的东西更有意义,并把它放在一个h1标签内(不知道为什么,这是有意义的)
Now you could turn this alert into something more meaningful, and put it within a h1 tag (not sure why this is meaningful)
$("body").html("<h1>"+data.name+"</h1>");
但有时候你会想检索数据,并用它做什么服务器端的应用程序。
But sometimes you would want to retrieve data and do something with it server side in your application.
因此,创建一个Django的URL模式和观点,例如:
So create a django urlpattern and view, e.g.:
from urllib2 import urlopen
from django.http import HttpResponse
from django.utils import simplejson
def call_bill(request):
url = "http://graph.facebook.com/bill.clinton"
json = urlopen(url).read()
# do whatever you want
return HttpResponse(simplejson.dumps(json), mimetype="application/json")
# add this to your url patterns
url("^call_bill_clinton/$", call_bill)
现在访问你的网址
作为一个逻辑结果,它也完全有可能通过某个用户操作触发异步事件。例如,在pviously提到的AJAX例子中,$ P $ URL参数,也可能是一个Django的网址,如/ call_bill_clinton /".
As a logic result, it's also perfectly possible to trigger async events by some user action. Eg the URL parameter in the previously mentioned ajax example, could also be a django url like "/call_bill_clinton/".
<!-- add a button to call the function -->
<button onclick="callFacebook();">Call Bill</button>
function callFacebook() {
$.ajax({
type: "GET",
data: ({}),
dataType: 'json',
url: "/call_bill_clinton/",
success: function(data){
alert("Hi I am former "+data.name+" and I came from Django");
}
});
)
// remove the auto call
此外Ajax调用,让你做同样的挂羊头卖狗肉的HTTP请求,您可以使用多种请求方法结合爽JavaScript事件,如beforeSend事件
Furthermore ajax calls let you do the same trickery as http requests, you can use a variety of request methods combined with cool javascript events, like a beforeSend event
beforeSend: function() {
$('#loading').show();
},
凡#loading可能是这样的:
Where the #loading could be something like:
<div id="loading" style="display:none;">
<img src="{% static "images/loading.gif" %}" />
</div>