最简单的方法是通过Ajax查询Django。必须定义一个返回一些数据(最好是json)的视图,然后从js脚本调用它。在from django.core import serializers
from django.http import HttpResponse
def all_questions(request):
"""
Most of the time you will have to do some extra work formating the json in
order to match the format that the JS library is expecting.
I can see your graph is expecting something like:
series: [{
name:
data:
}
],
So you need to provide to the JS library the data it is expecting.
That means some array (or perhaps similar data structure).
"""
questions = Questions.objects.all()
response = serializers.serialize('json', questions)
# Now response contain a representation of a
# json object that has a property called data
# besides, that property contain a list of Django objects.
response = "{data: %s}" % response
return HttpResponse(response, content_type="application/json")
现在,假设您在javascript中获得了数据(Ajax调用成功),您将得到如下内容:
{data: [list of serialized Django objects]}
您只需处理上面的列表并为您的图形提取数据。当然,您可以直接从Django视图获取该列表。这是你的决定。在
有关在图的series部分中放入什么的更多信息,请参阅Kendo demo。在
通过Ajax从JQuery查询Django。在
为此,您需要这样的代码:
^{pr2}$
1-请求方法的规范(GET、POST、UPDATE等)
2-指向视图的url。
请注意,这里有任何协议规范(http)。因为你的js生活在django应用程序中
url与此应用程序相关。为此,您需要一些url,如:urlpatterns = patterns('your_app.views',
...
url(r'url_to/you_view', 'your_view')
...
)
3-可选,一些要发送到de django视图的数据。(在你的情况下,你不需要它)
4-这很重要,在这里,当请求成功时,您将处理服务器返回的数据。
下面是一个json对象,如下所示:
{data: [...]}
其中[...]代表jason格式的django序列化对象的列表(参见文档链接)。在
5-On error将调用此函数,而不是success指定的函数。在
有关JQuery中$.ajax对象的更多信息,请参见$.ajax API Reference。在