Chartit不是有效的标签库:Django
我的 views.py 文件内容如下:
from django.shortcuts import render, render_to_response
from chartit import DataPool, Chart
from chartit.chartdata import DataPool
from weather.models import MonthlyWeatherByCity
import simplejson
from chartit import DataPool, Chart
def weather_chart_view(request):
ds=DataPool(series=[{'options': {'source': MonthlyWeatherByCity.objects.all()},'terms': ['month','houston_temp','boston_temp']}])
cht = Chart(datasource = ds, 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'}}})
return render_to_response('chart.html',{'weatherchart': cht})
应用里的 urls.py 文件内容如下:
from django.conf.urls import include, url
from django.contrib import admin
from weather import views
urlpatterns = [
url(r'^$', views.weather_chart_view , name='weather_chart_view')
]
models.py 文件内容如下:
from django.db import models
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)
chart.html 文件内容如下:
<head>
<!-- code to include the highcharts and jQuery libraries goes here -->
<!-- load_charts filter takes a comma-separated list of id's where -->
<!-- the charts need to be rendered to -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
<script src="/highcharts.js" type="text/javascript"></script>
{% load chartit %}
{{ weatherchart|load_charts:"container" }}
</head>
<body>
<div id='container'> {{ weatherchart|load_charts:"container" }}</div>
</body>
当我运行服务器并打开应用时,出现了这个错误:
TemplateSyntaxError at /weather/
'chartit' is not a valid tag library: ImportError raised loading chartit.templatetags.chartit: cannot import name simplejson
我已经在 INSTALLED_APPS 中包含了应用、chartit 和 json。
正如你所看到的,我在视图中也导入了 simplejson。我哪里出错了呢?
如果我需要提供其他信息来让问题更清楚,请告诉我。
3 个回答
2
在最新版本的chartit(0.2.8)中,这个问题已经解决了。所以你只需要做的就是像@MiaeKim提到的那样,把'chartit'添加到INSTALLED_APPS里。
7
你需要在 "settings.py" 文件中把
chartit
加到INSTALLED_APPS
里。INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'chartit', )
然后,按照 @akoshodi 的回复进行操作。
13
这个问题在项目的github页面上有解决办法。你可以先运行 pip install simplejson
来安装一个叫做simplejson的库。然后找到 chartit/templatetags/chartit.py
这个文件,替换里面关于simplejson导入的那一行,像下面这样。
from django import template
-from django.utils import simplejson
+import simplejson
from django.utils.safestring import mark_safe
虽然这看起来像是个临时解决办法,但在正式修复合并之前,这个方法是有效的。