Django/Chartit NameError: 全局名称'MonthlyWeatherByCity'未定义
我在我的Django网站上使用Chartit制作图表。按照官方教程操作,但在我尝试访问我的页面时遇到了以下错误。
NameError at /chart/ <br>
global name 'MonthlyWeatherByCity' is not defined
如果有人能给我一些建议来解决这个问题,我会非常感激。我使用的是Chartit的演示数据库,在启动服务器之前,我已经运行了python manage.py syncdb
。
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)
view.py的内容如下:
from chartit import DataPool,Chart
from django.template import RequestContext
from django.template import Context,loader
from django.shortcuts import render_to_response
from django.http import HttpResponse
def weather_chart_view(request):
#Step 1: Create a DataPool with the data we want to retrieve.
weatherdata = \
DataPool(
series=
[{'options': {
'source': MonthlyWeatherByCity.objects.all()},
'terms': [
'month',
'houston_temp',
'boston_temp']}
])
#Step 2: Create the Chart object
cht = Chart(
datasource = weatherdata,
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'}}})
#Step 3: Send the chart object to the template.
return render_to_response({'weatherchart': cht})
模板中的chart.html内容如下:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/highcharts.js"></script>
{% load chartit %}
{{ weatherchart|load_charts:"container" }}
</head>
<body>
<div id="container" style="width:100%; height:400px;"></div>
</body>
</html>
1 个回答
0
你还没有导入你的模型。在你的 views.py
文件的顶部添加以下内容:
from .models import MonthlyWeatherByCity