我的 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
,而不是load chartit
。也没有任何变化。 - praxmon