【发布时间】: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