【问题标题】:django 'LineChart' object has no attribute 'get'django 'LineChart' 对象没有属性 'get'
【发布时间】:2017-02-16 09:58:58
【问题描述】:

我正在尝试在 django 中制作折线图,我正在使用 jchart 模块,当我尝试加载图表时出现以下错误。

错误 “LineChart”对象没有“get”属性

完整的回溯

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/deprecation.py", line 138, in __call__
    response = self.process_response(request, response)
  File "/usr/local/lib/python2.7/dist-packages/django/middleware/clickjacking.py", line 32, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'LineChart' object has no attribute 'get'

查看

from jchart import Chart
from jchart.config import Axes, DataSet, rgba


class LineChart(Chart):
    chart_type = 'line'
    responsive = False
    scales = {
        'xAxes': [Axes(type='time', position='bottom')],
    }

    def get_datasets(self, **kwargs):
        data = [{'y': 0, 'x': '2017-01-02T00:00:00'}, {'y': 1, 'x': '2017-01-03T00:00:00'}, {'y': 4, 'x': '2017-01-04T00:00:00'}, {'y': 9, 'x': '2017-01-05T00:00:00'}, {'y': 16, 'x': '2017-01-06T00:00:00'}, {'y': 25, 'x': '2017-01-07T00:00:00'}, {'y': 36, 'x': '2017-01-08T00:00:00'}, {'y': 49, 'x': '2017-01-09T00:00:00'}, {'y': 64, 'x': '2017-01-10T00:00:00'}, {'y': 81, 'x': '2017-01-11T00:00:00'}, {'y': 100, 'x': '2017-01-12T00:00:00'}, {'y': 121, 'x': '2017-01-13T00:00:00'}, {'y': 144, 'x': '2017-01-14T00:00:00'}, {'y': 169, 'x': '2017-01-15T00:00:00'}, {'y': 196, 'x': '2017-01-16T00:00:00'}, {'y': 225, 'x': '2017-01-17T00:00:00'}, {'y': 256, 'x': '2017-01-18T00:00:00'}, {'y': 289, 'x': '2017-01-19T00:00:00'}, {'y': 324, 'x': '2017-01-20T00:00:00'}, {'y': 361, 'x': '2017-01-21T00:00:00'}, {'y': 400, 'x': '2017-01-22T00:00:00'}, {'y': 441, 'x': '2017-01-23T00:00:00'}, {'y': 484, 'x': '2017-01-24T00:00:00'}, {'y': 529, 'x': '2017-01-25T00:00:00'}, {'y': 576, 'x': '2017-01-26T00:00:00'}, {'y': 625, 'x': '2017-01-27T00:00:00'}, {'y': 676, 'x': '2017-01-28T00:00:00'}, {'y': 729, 'x': '2017-01-29T00:00:00'}, {'y': 784, 'x': '2017-01-30T00:00:00'}, {'y': 841, 'x': '2017-01-31T00:00:00'}, {'y': 900, 'x': '2017-02-01T00:00:00'}]

        return [DataSet(
            type='line',
            label='Time Series',
            data=data,
        )]

    def some_view(request):
        render(request, 'polls/chart.html', {
        'line_chart': LineChart(),
    })

网址

url(r'^polls/bubble/$', views.LineChart, name='bubble'),

chart.html

{% extends "base.html" %}
{% load jchart %}

 {% block content %}

{{ line_chart.as_html }}

</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
 {% endblock %}

【问题讨论】:

  • 我已经添加了完整的回溯

标签: javascript python django chart.js


【解决方案1】:

您应该在 URL 模式中使用视图 some_view,而不是类 LineChart。

url(r'^polls/bubble/$', views.some_view, name='bubble'),

另外,检查some_view 的缩进是否正确。这可能只是您的问题而不是您的实际代码中的错误,但您目前将 some_view 作为 LineChart 类的方法。应该是:

class LineChart(Chart):
    chart_type = 'line'
    responsive = False
    ...

def some_view(request):
    render(request, 'polls/chart.html', {
    'line_chart': LineChart(),
})

【讨论】:

    猜你喜欢
    • 2017-10-14
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2020-04-01
    • 2020-11-07
    相关资源
    最近更新 更多