在Django 1.6中使用Python 2.7实现图表 - TypeError:'NoneType'没有属性__getitem__ [英] Implement chartit in Django 1.6 with Python 2.7 - TypeError: 'NoneType' has no attribute __getitem__

查看:248
本文介绍了在Django 1.6中使用Python 2.7实现图表 - TypeError:'NoneType'没有属性__getitem__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



但是我只收到这个错误信息:

 异常值:'NoneType'对象没有属性'__getitem__'
异常位置:/home/administrator/virtualenvs/django27/lib/python2.7/site第128行的load_charts中的packages / chartit / templatetags / chartit.py

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



错误是否显示,render_to不是dict?



models.py:

 类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请求):
#Step 1:使用我们要检索的数据创建一个DataPool。
weatherdata = \
DataPool(
系列=
[{'options':{
'source':MonthlyWeatherByCity.objects.all()},
'terms':[
'month',
'houston_temp',
'boston_temp']}
])

#Step 2:创建图表对象
cht = Chart(
datasource = weatherdata,
series_options =
[{'options':{
'type':'line',
'stacking':False},
'terms':{
'month':[
'boston_temp',
'houston_temp']
}} ],
chart_options =
{'title':{
'text':'波士顿和休斯顿的天气数据'},
'xAxis': {
'title':{
'text':'月号'}}})

我在模板中添加了脚本文件{{load block}}。

 < div ID = '容器' > {{weatherchart | load_charts:container}}< / div> 

< script type =text / javascriptsrc =/ static / js / jquery-1.11.0.js>< / script>
< script type =text / javascriptsrc =/ static / js / highcharts.js>< / script>

但这在我看来没有问题。



如何解决?谢谢!

解决方案

在一段时间浪费之后,我认为解决方案是使您的'视图'返回的字典密钥你在你的模板html文件中有什么。例如,

  def weather_view(request):
#其他代码
返回render_to_response(' graph.html',{'weatherchart':cht})

那么你的html模板遵循上面的格式:

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

还要确保在您的...中安装 simplejson virtualenv使用:
pip install simplejson


I have successfully implemented some apps in Django. At the moment I try to implement a chart according to the tutorial: 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

Does the error indicate, that render_to is not a 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'}}})

I included the script files, the {{ load block }} in the templates ..

<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!

解决方案

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. 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:

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

Also make sure you install simplejson in your virtualenv using: pip install simplejson

这篇关于在Django 1.6中使用Python 2.7实现图表 - TypeError:'NoneType'没有属性__getitem__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆