在Django 1.6中使用Python 2.7实现chartit - TypeError: 'NoneType'没有属性__getitem__
我已经成功用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
经过一段时间的折腾,我觉得解决办法是让你在'view'中返回的字典键和你在模板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>
另外,确保你在虚拟环境中安装了 simplejson
,可以使用以下命令:
pip install simplejson
0
你应该按照下面的方式修改模板文件:
<!-- Include all the required script files here -->
{% load chartit %}
{{ weatherchart|load_charts:"container" }}
<div id="container">{{ weatherchart|load_charts:"container" }}</div>
0
试着把 {{ weatherchart|load_charts:"container" }}
这一行代码从 id 为 container
的 div
中移出来,放到 header
部分,就像这个例子所示的那样。