[英]Implement chartit in Django 1.6 with Python 2.7 : TypeError: 'NoneType' has no attribute __getitem__ - Need more information
I am referencing the stackoverflow question Implement chartit in Django 1.6 with Python 2.7 - TypeError: 'NoneType' has no attribute __getitem__ 我正在引用stackoverflow问题在python 1.6的Django 1.6中实现chartit-TypeError:'NoneType'没有属性__getitem__
I do not have enough karma to comment on the persons post or have the ability to contact them, but they have stated that: 我没有足够的业力来评论这些帖子或没有能力与他们联系,但他们表示:
"I have fixed the problem. The Problem was indeed at the line hco['chart']['renderTo'] = render_to. I had to fix some data consistency problems, and now it worked." “我已经解决了这个问题。问题确实在hco ['chart'] ['renderTo'] = render_to行。我不得不解决一些数据一致性问题,现在可以了。”
What are these data consistency issues? 这些数据一致性问题是什么? It must be related to the newer version of django.
它必须与django的较新版本相关。
Following the tutorial http://chartit.shutupandship.com/docs/ 按照教程http://chartit.shutupandship.com/docs/
I am having the same error: 'NoneType' object has no attribute ' getitem ' 我遇到了同样的错误:'NoneType'对象没有属性' getitem '
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)
def __unicode__(self):
return unicode(self.month)
views.py 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 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>
Debugging page 调试页面
/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
[]**
I have attached the chartit.py code for your viewing purposes. 我已附上chartit.py代码以供您查看。 I am not sure what data consistences need to be fixed / changed.
我不确定需要修复/更改哪些数据一致性。
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)
The name 'weatherchart' isn't associated with a chart object. 名称“ Weatherchart”与图表对象没有关联。
At the end of the view function you should have: 在视图功能的最后,您应该具有:
return render_to_response(<template>, {'<chart_name>': <chart_object>})
And in the template (index.html in this case): 并在模板中(本例中为index.html):
{{ <chart_name>|load_charts:"container" }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.