【问题标题】:When trying to display a chart using ChartIt, Error saying template does not exist arises尝试使用 ChartIt 显示图表时,出现模板不存在的错误
【发布时间】:2014-07-23 04:26:53
【问题描述】:

我正在使用ChartIt 并在尝试查看 URL 上的图表时不断收到“模板不存在”。本教程正在正确遵循,但可能在某处犯了错误。我是 Django 的新手,所以任何帮助将不胜感激。在调用 def 时,加载图表的请求正在工作。

views.py 文件中的定义。

def lineChart(request):
    commitData = \
        DataPool(
            series=
            [{'options': {
                'source': TestCommit.objects.all()[:200]}, 'terms': ['author', 'author_time']}])

    linechart = Chart(
        datasource=commitData,
        series_options=
        [{'options': {
            'type': 'line',
            'stacking': False},
          'terms': {
              'author_time': [
                  'author_time']
          }}],
        chart_options=
        {'title': {
            'text': 'YAYs'},
         'xAxis': {
             'title': {
                 'text': 'Month number'}}})


    return render_to_response({'testCommits.html': linechart})

testCommits.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 %}
    {{ linechart|load_charts:"container" }}
</head>
<body>
    <div id='container'> Chart will be rendered here </div>
</body>

【问题讨论】:

  • 您是否在urls.py 中映射了您的视图?尝试看看您是否可以使用浏览器访问您的视图。
  • 是的,url(r'^lineChart/', dashboard.views.lineChart),
  • 我知道它在视图中命中了 def,因为当它在浏览器中加载它时,我可以在堆栈跟踪中看到错误。
  • 可能与您的问题无关,但您的视图返回错误。应该是return render_to_response('testCommits.html', linechart)
  • 根据指南,这是不正确的,会导致语法错误。

标签: python django highcharts


【解决方案1】:

我注意到在他们的安装说明中,他们没有说要在设置文件中的 INSTALLED_APPS 中添加“chartit”,但他们在演示项目中这样做了。你做到了吗?

编辑: 丹克拉松是正确的。您需要指定要返回的模板。您的视图应返回以下内容:

return render_to_response('testCommits.html', {'linechart': linechart})

字典是传递给模板的上下文。第一个参数是模板名称,在您的例子中是 testCommits.html。

【讨论】:

  • testCommits.html 模板是否在文件夹中?你能成功渲染其他模板吗?
【解决方案2】:

您收到此错误是因为您的 Django 在模板加载期间不知道模板的位置。试试这个:

  1. 转到您的项目根目录中的 settings.py 并指定您的 TEMPLATE_DIRS。该变量已为您定义,因此只需列出 path_to_your_dir。例如:

    TEMPLATE_DIRS = (
            "home/myname/path_to_templates"
    )
    
  2. 更新您的视图以按照其他用户的指定正确返回:

    return render_to_response('testCommits.html', {'linechart': linechart})
    

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    相关资源
    最近更新 更多