简体   繁体   English

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

[英]Implement chartit in Django 1.6 with Python 2.7 - TypeError: 'NoneType' has no attribute __getitem__

I have successfully implemented some apps in Django. 我已经在Django中成功实现了一些应用程序。 At the moment I try to implement a chart according to the tutorial: http://chartit.shutupandship.com/docs/#how-to-use . 目前我尝试根据教程实现图表: http//chartit.shutupandship.com/docs/#how-to-use

But I only receive this error message: 但我只收到此错误消息:

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

The failure appears in this line: hco['chart']['renderTo'] = render_to 失败出现在这一行:hco ['chart'] ['renderTo'] = render_to

Does the error indicate, that render_to is not a dict? 错误是否表明,render_to不是dict?

models.py: 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: 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'}}})

I included the script files, the {{ load block }} in the templates .. 我在模板中包含了脚本文件{{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>

but that's in my opinion not the problem. 但在我看来,这不是问题所在。

How can I fix it? 我该如何解决? Thanks! 谢谢!

尝试使用id container{{ weatherchart|load_charts:"container" }}行移出div并进入header ,就像示例一样。

you should modify the template file as below : 你应该修改模板文件如下:

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

    {% load chartit %}

    {{ weatherchart|load_charts:"container" }}

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

After some good time wasted, I think the solution is to make the dictionary key returned by your 'view' matche what you have in your template html file. 经过一段时间的浪费,我认为解决方案是让你的'view'matche返回你的模板html文件中的字典键。 For instance, 例如,

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

then your html template follows format as above: 然后你的html模板遵循以上格式:

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

Also make sure you install simplejson in your virtualenv using: pip install simplejson 另外,还要确保你安装simplejson在你的virtualenv使用: pip install simplejson

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Django 1.6中使用Python 2.7实现chartit:TypeError:&#39;NoneType&#39;没有属性__getitem__-需要更多信息 - Implement chartit in Django 1.6 with Python 2.7 : TypeError: 'NoneType' has no attribute __getitem__ - Need more information Django错误:TypeError:&#39;NoneType&#39;对象没有属性&#39;__getitem__&#39; - Django error: TypeError: 'NoneType' object has no attribute '__getitem__' typeError:&#39;NoneType&#39;对象没有属性&#39;__getitem__&#39;python - typeError: 'NoneType' object has no attribute '__getitem__' python Python TypeError:“ NoneType”对象没有属性“ __getitem__” - Python TypeError: 'NoneType' object has no attribute '__getitem__' Python:TypeError:“ NoneType”对象没有属性“ __getitem__” - Python: TypeError: 'NoneType' object has no attribute '__getitem__' python TypeError:&#39;NoneType&#39;对象没有属性&#39;__getitem__&#39; - python TypeError: 'NoneType' object has no attribute '__getitem__' Python TypeError:“ NoneType”对象的Google搜索没有属性“ __getitem__” - Python TypeError: 'NoneType' object has no attribute '__getitem__' for Google Search Python — TypeError:&#39;NoneType&#39;对象没有属性&#39;__getitem__&#39; - Python — TypeError: 'NoneType' object has no attribute '__getitem__' Python TypeError:“ NoneType”对象没有属性“ __getitem__” - Python TypeError: 'NoneType' object has no attribute '__getitem__ Python TypeError: 'NoneType' object has no attribute '__getitem__' 错误 - Python TypeError: 'NoneType' object has no attribute '__getitem__' Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM