我是Django的新手。我想用django graphos绘制谷歌图。我已经为此写了一些小代码,我得到了空模板。任何领导都会受到批评..
view.py
from graphos.renderers import gchart
from django.shortcuts import render
from django.shortcuts import render_to_response
from graphos.sources.model import ModelDataSource
from models import MyCityViews
def get_context_data(self):
queryset = MyCityViews.objects.all()
data_source = ModelDataSource(queryset,fields=['city', 'views'])
line_chart = gchart.LineChart(data_source)
context = {"chart": line_chart}
return render_to_response('gchart.html', {'chart': line_chart})
models.py
from django.db import models
# Create your models here.
class MyCityViews(models.Model):
city = models.CharField(max_length = 30, blank = True , null = True)
views = models.IntegerField()
class Meta:
ordering = ['city']
verbose_name = 'city'
verbose_name_plural = 'city'
def __unicode__(self):
return self.city
gchart.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>Graphos</title>
{% load static from staticfiles %}
<link rel="stylesheet" href="{% static "css/bootstrap.css" %}">
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback({{line_chart.as_html}});
</script>
</head>
<body>
{{line_chart.as_html}}
<div id="container"></div>
</body>
</html>
我做错了......
答案 0 :(得分:0)
没有将视图对象传递给div块,这就是为什么我没有得到HTML视图。
<body>
<div id="container">
{{line_chart.as_html}}
</div>
</body>