在Django 1.6和Python 2.7中实现chartit:TypeError:'NoneType'没有属性__getitem__ - 需要更多信息
我在参考一个StackOverflow的问题,链接是 在Django 1.6中用Python 2.7实现chartit - 类型错误:'NoneType'没有属性 __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)
1 个回答
0
名字 'weatherchart' 没有和任何图表对象关联起来。
在视图函数的最后,你应该有:
return render_to_response(<template>, {'<chart_name>': <chart_object>})
在模板中(这里是 index.html):
{{ <chart_name>|load_charts:"container" }}