使用Python 2.7在Django 1.6中实现chartit - TypeError:'NoneType'没有属性__getitem__

时间:2014-03-13 09:49:47

标签: python django highcharts

我已经在Django中成功实现了一些应用程序。目前我尝试根据教程实现图表:http://chartit.shutupandship.com/docs/#how-to-use

但我只收到此错误消息:

Exception Value: 'NoneType' object has no attribute '__getitem__'
Exception Location: /home/administrator/virtualenvs/django27/lib/python2.7/site-packages/chartit/templatetags/chartit.py in load_charts, line 68

此行中出现故障:     hco ['chart'] ['renderTo'] = render_to

错误是否表明,render_to不是dict?

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)

views.py:

def weather_chart_view(request):
    #Step 1: Create a DataPool with the data we want to retrieve.
    weatherdata = \
        DataPool(
           series=
            [{'options': {
               'source': MonthlyWeatherByCity.objects.all()},
              'terms': [
               'month',
               'houston_temp',
               'boston_temp']}
             ])

    #Step 2: Create the Chart object
    cht = Chart(
            datasource = weatherdata,
            series_options =
              [{'options':{
                  'type': 'line',
                  'stacking': False},
                'terms':{
                  'month': [
                    'boston_temp',
                    'houston_temp']
                  }}],
            chart_options =
              {'title': {
                   'text': 'Weather Data of Boston and Houston'},
               'xAxis': {
                    'title': {
                       'text': 'Month number'}}})

我在模板中包含了脚本文件{{load block}} ..

<div id='container'> {{ weatherchart|load_charts:"container" }} </div>

<script type="text/javascript" src="/static/js/jquery-1.11.0.js"></script>
<script type="text/javascript" src="/static/js/highcharts.js"></script>

但我认为这不是问题。

我该如何解决?谢谢!

3 个答案:

答案 0 :(得分:0)

尝试将{{ weatherchart|load_charts:"container" }}行移出div,其代码为container,然后移至header,与example一样。

答案 1 :(得分:0)

您应该修改模板文件,如下所示:

    <!-- Include all the required script files here -->

    {% load chartit %}

    {{ weatherchart|load_charts:"container" }}

    <div id="container">{{ weatherchart|load_charts:"container" }}</div>

答案 2 :(得分:0)

浪费了一些好时间之后,我认为解决方案是让你的'view'matche返回你在模板html文件中所拥有的字典键。例如,

   def weather_view(request):
       # other codes
       return render_to_response('charts/graph.html', {'weatherchart': cht})

然后您的html模板遵循以上格式:

    <div id='container'> {{ weatherchart|load_charts:"container" }} </div>

还要确保使用以下命令在virtualenv中安装simplejson:         pip install simplejson