You haven't imported your model. At the top of your views.py
add:
from .models import MonthlyWeatherByCity
Question
I am trying to product plots in my Django site using Chartit. I am following the official tutorial, but getting the following error as I try to navigate to my page.
NameError at /chart/ <br>
global name 'MonthlyWeatherByCity' is not defined
I would appreciate any tips that might solve this issue. I am using the demo database from Chartit, I have run python manage.py syncdb
before I started the server.
models.py looks like:
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 looks like:
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 in template looks like:
<!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>
Solution
You haven't imported your model. At the top of your views.py
add:
from .models import MonthlyWeatherByCity