Chartit不是一个有效的标签库:Django。

5

我的 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

我已经将应用程序、chartit和json包含在INSTALLED_APPS中。 如您所见,我还在视图中导入了simplejson。 我错在哪里?请建议是否需要发布其他内容以使问题更加清晰。

你尝试过 {% load charts %} 吗?在 chartit 的文档中,使用方法第四点说到:使用 {% load_charts %} 模板标签将图表加载到具有特定 ID 的 HTML 标签中。你确定你的已安装应用列表中真的有 'simplejson' 应用吗? - KhoPhi
你能详细解释需要做什么吗?如果你在谈论HTML文件,我的文件已经有了。 - praxmon
请执行以下操作:不要在您的模板文件中使用 {% load chartit %},而是使用 {% load charts %}。这里出现了两个问题,我不知道它们之间是否相互关联,但很可能是未安装 'simplejson' 应用程序,或者如果已安装,可能需要使用 'python manage.py syncdb' 命令刷新数据库。 - KhoPhi
@Rexford 尝试了 syncdb。没有任何变化。还尝试使用load charts,而不是load chartit。也没有任何变化。 - praxmon
顺便说一下,你在views.py中导入了"simplejson",但是没有使用它。为什么? - KhoPhi
显示剩余2条评论
3个回答

13

在该项目的Github页面上有一个解决该问题的修复方法。首先执行pip install simplejson,然后定位chartit模块中的chartit/templatetags/chartit.py文件,并按照下面所示的替换simplejson导入行。

from django import template
-from django.utils import simplejson
+import simplejson
from django.utils.safestring import mark_safe

看起来像是黑客攻击,但在修复合并之前都能使用。


7
  1. You need to add chartit to INSTALLED_APPS in "settings.py"

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'chartit',
    )
    
  2. Then, follow @akoshodi 's response.


2
在最新版本的 chartit (0.2.8) 中,问题已经被修复,所以您需要做的唯一一件事是按照 @MiaeKim 的提示,在 INSTALLED_APPS 中添加 'chartit'。请注意保留 HTML 标签。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接