Python 尝试使用ChartIt显示图表时,会出现错误,说明模板不存在

Python 尝试使用ChartIt显示图表时,会出现错误,说明模板不存在,python,django,highcharts,Python,Django,Highcharts,我正在使用并不断收到的模板不存在时,试图查看图表的网址。本教程正在正确遵循,但可能在某个地方出错。我是Django的新手,所以任何帮助都将不胜感激。在调用def时,加载图表的请求正在工作 在views.py文件中输入def def lineChart(request): commitData = \ DataPool( series= [{'options': { 'source': Test

我正在使用并不断收到的模板不存在时,试图查看图表的网址。本教程正在正确遵循,但可能在某个地方出错。我是Django的新手,所以任何帮助都将不胜感激。在调用def时,加载图表的请求正在工作

在views.py文件中输入def

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>

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

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

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

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

由于Django在模板加载过程中不知道模板的位置,因此出现此错误。试试这个:

转到项目根目录中的settings.py并指定模板目录。变量已经为您定义,所以只需列出路径到您的目录即可。例如:

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

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

您是否在URL.py中映射视图?尝试查看您是否可以使用浏览器访问视图。是的,urlr“^lineChart/”,dashboard.views.lineChart,我知道它在视图中遇到了def,因为在浏览器中加载堆栈跟踪时,我可以在堆栈跟踪中看到错误。可能与您的问题无关,但返回的视图是错误的。根据指导原则,应该将render_返回到响应'testCommits.html',linechart,这是不正确的,并且会导致语法错误。testCommits.html模板是否在文件夹中?能否成功呈现其他模板?