Joh*_*n Z 6 python django graph flot
下面的教程(https://github.com/agiliq/django-graphos)和这个stackoverflow文章(使用Django-graphos显示图形)我无法获得任何数据发布到我的模板.
models.py
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)
Run Code Online (Sandbox Code Playgroud)
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})
Run Code Online (Sandbox Code Playgroud)
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 %}
Run Code Online (Sandbox Code Playgroud)
./manage.py shell
当我将所有内容复制到shell中时 - 定义request ="test" - 并运行print graph_test(request)
<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>
Run Code Online (Sandbox Code Playgroud)
为{{chart.as_html}}设置了这个位置.我不确定为什么没有导入我的模板.
你没有显示render_to_response来自哪里。我怀疑该功能可能是导致问题的部分原因。
请注意,普通render_to_response函数已被弃用,取而代之的是显式处理对象的render函数request。
import django.shortcuts\n\nfrom graphos.sources.model import ModelDataSource\nimport graphos.renderers\n\nfrom portal.models import MonthlyWeatherByCity\n\n\ndef graph_test(request):\n queryset = MonthlyWeatherByCity.objects.all()\n data_source = ModelDataSource(queryset, fields=[\n \'boston_temp\', \'houston_temp\'])\n chart = graphos.renderers.flot.LineChart(data_source, options={\xe2\x80\xa6})\n return django.shortcuts.render(\n request,\n template_name=\'portal/index.html\',\n context={\'chart\': chart})\nRun Code Online (Sandbox Code Playgroud)\n\n另请注意,建议使用基于类的视图,因为它更灵活。
\n