我正在引用stackoverflow问题Implement chartit in Django 1.6 with Python 2.7 - TypeError: 'NoneType' has no attribute __getitem__
我没有足够的业力来评论这些人的帖子或有能力与他们联系,但他们已经声明:
“我已经修复了问题。问题确实存在于行hco ['chart'] ['renderTo'] = render_to。我不得不修复一些数据一致性问题,现在它工作“。
这些数据一致性问题是什么?它必须与较新版本的django相关。
按照教程http://chartit.shutupandship.com/docs/
我遇到了同样的错误:'NoneType'对象没有属性' getitem '
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)
def __unicode__(self):
return unicode(self.month)
views.py
def weather_chart_view(request):
#Step 1: Create a DataPool with the data we want to retrieve.
context = RequestContext(request)
weatherdata = \
DataPool(
series=
[{'options': {
'source': MonthlyWeatherByCity.objects.all()},
'terms': [
'month',
'houston_temp',
'boston_temp']}
])
#Step 2: Create the Chart object
chart_list = 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'}}})
的index.html
<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>
调试页面
/home/nimbus/.virtualenvs/nimbus_portal/lib/python2.7/site-packages/chartit/templatetags/chartit.py in load_charts
hco['chart']['renderTo'] = render_to ...
▼ Local vars
Variable Value
**chart_list
[]**
我附上了chartit.py代码供您查看。我不确定需要修复/更改哪些数据一致性。
chartit.py
from itertools import izip_longest
from django import template
from django.utils import simplejson
from django.utils.safestring import mark_safe
from django.conf import settings
import posixpath
from ..charts import Chart, PivotChart
try:
CHARTIT_JS_REL_PATH = settings.CHARTIT_JS_REL_PATH
if CHARTIT_JS_REL_PATH[0] == '/':
CHARTIT_JS_REL_PATH = CHARTIT_JS_REL_PATH[1:]
CHART_LOADER_URL = posixpath.join(settings.STATIC_URL,
CHARTIT_JS_REL_PATH,
'chartloader.js')
except AttributeError:
CHARTIT_JS_REL_PATH = 'chartit/js/'
CHART_LOADER_URL = posixpath.join(settings.STATIC_URL,
CHARTIT_JS_REL_PATH,
'chartloader.js')
register = template.Library()
@register.filter
def load_charts(chart_list=None, render_to=''):
"""Loads the ``Chart``/``PivotChart`` objects in the ``chart_list`` to the
HTML elements with id's specified in ``render_to``.
:Arguments:
- **chart_list** - a list of Chart/PivotChart objects. If there is just a
single element, the Chart/PivotChart object can be passed directly
instead of a list with a single element.
- **render_to** - a comma separated string of HTML element id's where the
charts needs to be rendered to. If the element id of a specific chart
is already defined during the chart creation, the ``render_to`` for that
specific chart can be an empty string or a space.
For example, ``render_to = 'container1, , container3'`` renders three
charts to three locations in the HTML page. The first one will be
rendered in the HTML element with id ``container1``, the second
one to it's default location that was specified in ``chart_options``
when the Chart/PivotChart object was created, and the third one in the
element with id ``container3``.
:returns:
- a JSON array of the HighCharts Chart options. Also returns a link
to the ``chartloader.js`` javascript file to be embedded in the webpage.
The ``chartloader.js`` has a jQuery script that renders a HighChart for
each of the options in the JSON array"""
embed_script = (
'<script type="text/javascript">\n'
'var _chartit_hco_array = %s;\n</script>\n'
'<script src="%s" type="text/javascript">\n</script>')
if chart_list is not None:
if isinstance(chart_list, (Chart, PivotChart)):
chart_list = [chart_list]
chart_list = [c.hcoptions for c in chart_list]
render_to_list = [s.strip() for s in render_to.split(',')]
for hco, render_to in izip_longest(chart_list, render_to_list):
if render_to:
hco['chart']['renderTo'] = render_to
embed_script = (embed_script % (simplejson.dumps(chart_list,
use_decimal=True),
CHART_LOADER_URL))
else:
embed_script = embed_script %((), CHART_LOADER_URL)
return mark_safe(embed_script)
答案 0 :(得分:0)
名称'weatherchart'与图表对象无关。
在视图功能结束时,您应该:
return render_to_response(<template>, {'<chart_name>': <chart_object>})
在模板中(本例中为index.html):
{{ <chart_name>|load_charts:"container" }}