Python 使用Django graphos-Django 1.6显示图形
在教程()和这个stackoverflow post()之后,我无法获取任何要发布到模板中的数据 models.pyPython 使用Django graphos-Django 1.6显示图形,python,django,graph,flot,Python,Django,Graph,Flot,在教程()和这个stackoverflow post()之后,我无法获取任何要发布到模板中的数据 models.py class MonthlyWeatherByCity(models.Model): month = models.IntegerField() boston_temp = models.DecimalField(max_digits=5, decimal_places=1) houston_temp = models.DecimalField(max_di
class MonthlyWeatherByCity(models.Model):
month = models.IntegerField()
boston_temp = models.DecimalField(max_digits=5, decimal_places=1)
houston_temp = models.DecimalField(max_digits=5, decimal_places=1)
class Meta:
verbose_name_plural = "Monthly Weather By Cities"
def __unicode__(self):
return unicode(self.month)
views.py
from graphos.sources.model import ModelDataSource
from graphos.renderers import flot
from portal.models import MonthlyWeatherByCity
def graph_test(request):
queryset = MonthlyWeatherByCity.objects.all()
data_source = ModelDataSource(queryset, fields=['boston_temp', 'houston_temp'])
chart = flot.LineChart(data_source, options={'title': "Website", 'xaxis': {'mode': "categories"}})
return render_to_response('portal/index.html', {'chart': chart})
index.html-图表不会显示任何内容
{% extends 'portal/base.html' %}
{% load static %}
{% block head %}
<!-- Needed for graphos -->
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/flot/0.8.2/jquery.flot.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/flot/0.8.2/jquery.flot.categories.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
{% endblock %}
{% block body_block %}
<div class="hero-unit">
<h1>Attack Control Center</h1>
{% if user.is_authenticated %}
<h3>Welcome back {{ user.username }}!</h3>
{% endif %}
</div>
<div class="row-fluid">
<div class="span6">
<h2>Charts</h2>
{{ chart.as_html }}
</div>
</div>
{% endblock %}
{%extends'portal/base.html%}
{%load static%}
{%block head%}
{%endblock%}
{%block body_block%}
攻击控制中心
{%if user.u经过身份验证%}
欢迎回来{{user.username}}!
{%endif%}
图表
{{chart.as_html}
{%endblock%}
/manage.py shell
当我运行时,将所有内容复制到shell并-define request=“test”-并运行print graph\u test(request)
$(函数(){
美元绘图(
美元(“#ZGScakAPkH”),
,
{“系列”:{“线条”:{“显示”:“真实”},“图例”:{“位置”:“ne”},“xaxis”:{“模式”:“类别”},“标题”:“网站”}
);
});
这是为{{chart.as_html}设置的。我不确定为什么没有将其导入我的模板。您不会显示
呈现到\u响应的来源。我怀疑该功能可能是导致问题的原因之一
请注意,为了支持显式处理请求
对象的方法,使用了plain
导入django.shortcuts
从graphos.sources.model导入ModelDataSource
导入graphos.renders
从portal.models导入MonthlyWeatherByCity
def图形_测试(请求):
queryset=MonthlyWeatherByCity.objects.all()
数据源=模型数据源(查询集,字段=[
“波士顿临时工”、“休斯顿临时工”])
chart=graphos.renders.flot.LineChart(数据源,选项={…})
返回django.shortcuts.render(
要求
模板_name='portal/index.html',
上下文={'chart':chart})
还要注意的是,建议使用更灵活的方法
<div id="ZGScakAPkH" style="width:800px;height:400px;"></div>
<script type="text/javascript">
$(function () {
$.plot(
$("#ZGScakAPkH"),
,
{"series": {"lines": {"show": "true"}}, "legend": {"position": "ne"}, "xaxis": {"mode": "categories"}, "title": "Website"}
);
});
</script>
</div>