【问题标题】:Django/Chartit NameError: global name 'MonthlyWeatherByCity' is not definedDjango/Chartit NameError:未定义全局名称“MonthlyWeatherByCity”
【发布时间】:2014-04-30 02:52:02
【问题描述】:

我正在尝试使用 Chartit 在我的 Django 站点中制作绘图。我正在关注官方教程,但在尝试导航到我的页面时出现以下错误。

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>

【问题讨论】:

    标签: python django highcharts


    【解决方案1】:

    您尚未导入模型。在您的views.py 顶部添加:

    from .models import MonthlyWeatherByCity
    

    【讨论】:

    • 我相信你的意思是.models。它解决了原始问题,但我现在收到以下消息。 TemplateDoesNotExist at /chart/ 然后{'weatherchart': &lt;chartit.charts.Chart object at 0x7f02cc02fc50&gt;}
    • 您需要在视图代码的最后一行传递您的模板名称。因此,更改为:return render_to_response('chart.html', {'weatherchart': cht})。此外,.models 也不错。我会编辑它。
    • 现在就是这样...return render_to_response('chart.html',{'weatherchart': cht}) ...但我得到了这个 TypeError -- __init__() got an unexpected keyword argument 'use_decimal'
    • 看起来其他一些人(即here)也有类似的问题。我们离原来的问题还很远,所以我要开始一个新问题。
    • 是的,我也这么认为。我也看到其他人也面对过。我可能会创建另一个问题。
    猜你喜欢
    • 2011-04-27
    • 1970-01-01
    • 2014-10-24
    • 2012-05-29
    • 2013-08-23
    • 2014-04-03
    • 2017-05-12
    • 2015-09-01
    相关资源
    最近更新 更多