python - 使用 Django-graphos 显示图形 - django 1.6

标签 python django graph flot

以下教程 ( https://github.com/agiliq/django-graphos ) 和这篇 stackoverflow 帖子 ( Displaying graphs using Django-graphos ) 我无法获取任何数据以发布到我的模板。

模型.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)

View .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 %}

./manage.py 外壳

当我运行将所有内容复制到 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>

为 {{ chart.as_html }} 准备好了。我不确定为什么没有将其导入到我的模板中。

最佳答案

您没有显示 render_to_response 的来源。我怀疑该功能可能是导致问题的部分原因。

请注意,普通的 render_to_response function is deprecated , 赞成 render function显式处理 request 对象。

import django.shortcuts

from graphos.sources.model import ModelDataSource
import graphos.renderers

from portal.models import MonthlyWeatherByCity


def graph_test(request):
    queryset = MonthlyWeatherByCity.objects.all()
    data_source = ModelDataSource(queryset, fields=[
            'boston_temp', 'houston_temp'])
    chart = graphos.renderers.flot.LineChart(data_source, options={…})
    return django.shortcuts.render(
            request,
            template_name='portal/index.html',
            context={'chart': chart})

另请注意 class-based views被推荐为更灵活。

关于python - 使用 Django-graphos 显示图形 - django 1.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23097317/

相关文章:

python - Django manage.py 命令在 ElasticBeanstalk 上有 SyntaxError

django - 如何将 url 模式命名组与通用 View 一起使用?

python - MySQL 存在操作错误,而 SQLite3 没有

python - 如何使用 Python 将图形数字化?

algorithm - 如何计算搜索图形的最小预期时间?

python - 查找图中所有有循环的连接节点

python - 如何在Python中加入列表的相应元素

python - 如何在 __init__.py 中使用 import * 将文档字符串引入包范围?

python - 如何使用matplotlib获取包含多个图形的多个窗口?

python - 沿坐标列表给定的路径矢量化 haversine 距离计算