Displaying graphs using Django-graphos - django 1.6
Asked Answered
B

1

8

Following tutorial (https://github.com/agiliq/django-graphos) and this stackoverflow post (Displaying graphs using Django-graphos) I am unable to get any data to post to my template.

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)

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 - nothing displays for the chart whatsoever.

{% 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

When I run copy everything into the shell and - define request="test" - and run 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>

That get's put in place for the {{ chart.as_html }}. I am not sure why this is not being imported to my template.

Biology answered 16/4, 2014 at 1:18 Comment(0)
O
0

You don't show where render_to_response comes from. I suspect that function may be part of what's causing the problem.

Be aware that the plain render_to_response function is deprecated, in favour of the render function that explicitly handles the request object.

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})

Also be aware that class-based views are recommended as more flexible.

Oulu answered 11/6, 2017 at 2:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.