2014-04-16 4 views
6

निम्नलिखित ट्यूटोरियल (https://github.com/agiliq/django-graphos) और यह stackoverflow पोस्ट (Displaying graphs using Django-graphos) मैं अपने टेम्पलेट पर पोस्ट करने के लिए कोई डेटा प्राप्त करने में असमर्थ हूं।Django-graphos का उपयोग कर ग्राफ प्रदर्शित करना - django 1.6

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 - जो भी चार्ट के लिए कुछ भी नहीं प्रदर्शित करता है।

{% 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 जब मैं चलाने के खोल में सब कुछ कॉपी और शैल

- परिभाषित अनुरोध = "परीक्षण" - और प्रिंट graph_test (अनुरोध)

<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}} के लिए जगह में रखें। मुझे यकीन नहीं है कि यह मेरे टेम्पलेट में क्यों आयात नहीं किया जा रहा है।

उत्तर

0

आप यह नहीं दिखाते कि 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 अधिक लचीला के रूप में अनुशंसित हैं।

संबंधित मुद्दे