【问题标题】:Chartit is not a valid tag library:DjangoChartit 不是一个有效的标签库:Django
【发布时间】:2014-05-09 12:19:59
【问题描述】:

我的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 中包含了 app、chartit 和 json。

如您所见,我还在视图中导入了 simplejson。我哪里错了?

请建议我是否需要发布任何其他内容以使问题清晰。

【问题讨论】:

  • 你试过 {% load charts %} 吗?在 chartit 的文档,如何使用,第 4 点中,它说:使用 {% load_charts %} 模板标签将图表加载到具有特定 id 的 HTML 标签。您确定您的已安装应用列表中确实有“simplejson”应用吗?
  • 你能更详细地解释需要做什么吗? Ans 如果你在谈论 HTML 文件,我的文件已经有了。
  • 这样做:不要在上面的模板文件中使用 {% load chartit %},而是使用 {% load charts %}。它在那里抱怨两件事,我不知道它们是如何相互关联的,但很可能没有安装“simplejson”应用程序。或者如果安装了,也许它需要'python manage.py syncdb'命令来刷新数据库。
  • @Rexford 尝试了同步数据库。没变。还尝试了load charts 而不是load chartit。没有变化。
  • 顺便说一句,您在 views.py 中导入了“simplejson”,但没有使用它。为什么?

标签: python django django-templates


【解决方案1】:

在项目的 github 页面上有一个问题的修复。做一个pip install simplejson,然后在chartit模块中找到chartit/templatetags/chartit.py文件,替换simplejson import这一行,如下图。

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

它看起来像一个 hack,但在修复被合并之前可以工作。

【讨论】:

    【解决方案2】:
    1. 你需要在“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',
      )
      
    2. 然后,关注@akoshodi 的回复。

    【讨论】:

      【解决方案3】:

      在最新版本的 chartit (0.2.8) 问题已得到修复,因此唯一您应该做的就是添加 ' INSTALLED_APPS 中的chartit',正如@MiaeKim 提到的那样。

      【讨论】:

        猜你喜欢
        • 2011-12-20
        • 2010-12-13
        • 2012-10-16
        • 1970-01-01
        • 2012-03-21
        • 2015-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多