Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript嵌入代码中使用django变量(sqlite3数据库中的元素数组)_Javascript_Python_Django_Sqlite_Highcharts - Fatal编程技术网

在javascript嵌入代码中使用django变量(sqlite3数据库中的元素数组)

在javascript嵌入代码中使用django变量(sqlite3数据库中的元素数组),javascript,python,django,sqlite,highcharts,Javascript,Python,Django,Sqlite,Highcharts,基于此Highcharts示例(HTML中包含的javascript代码): 我有一个模板,我想在其中嵌入JavaScript代码,而不必包含任何静态代码。浏览器正在处理与JS无关的任何内容。当前my views.py的外观如下: # -*- encoding: utf-8 -*- from django.shortcuts import render from django.http import HttpResponse from tfgplot.models import estado_

基于此Highcharts示例(HTML中包含的javascript代码):

我有一个模板,我想在其中嵌入JavaScript代码,而不必包含任何静态代码。浏览器正在处理与JS无关的任何内容。当前my views.py的外观如下:

# -*- encoding: utf-8 -*-

from django.shortcuts import render
from django.http import HttpResponse
from tfgplot.models import estado_foneras
from django.template import RequestContext, loader

from tfgplot.models import estado_foneras

def index(request):
    clave = estado_foneras.objects.order_by('clave')
    template = loader.get_template('tfgplot/index.html')
    context = RequestContext(request, {
        'clave': clave,
    })
    return render(request, 'tfgplot/index.html', context)
如果我使用以下代码,它会完美地绘制,我会得到我所期望的:

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>
  </head>
  <body>
    <div id="container" style="min-width: 300px; height: 300px; margin: 1em">
    </div>
    <script type="text/javascript">
      {% autoescape off %}
      $(document).ready(function(){
          $('#container').highcharts({
              xAxis: {
                  categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
                      'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
              },
              series: [{
                  data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
              }]
          });
      });
      {% endautoescape %}
    </script>
  </body>
</html>

有什么建议可以让我使用所有这些数据库值作为X轴和Y轴的数组吗?

您发送到模板的唯一变量是“clave”,但您尝试使用一个名为“classK”的变量。也许您没有将变量发送到您认为自己是的模板?

您的{{classK.attr1}}和{{classK.attr2}看起来如何?您是否将其作为javascript数组或字符串返回?My models.py看起来像:#--编码:utf-8--来自django.db导入模型类classK(models.Model):attr1=models.CharField(max_length=6)attr2=models.SmallIntegerField()什么Sebastian(和我)想知道是在HTML页面生成的输出-你能粘贴这个吗?不是您的模型,也不是其他python代码。我能够访问模型类k中的所有字段(我有9个,我可以在index.html中读取它们)。我可以使用clave(=models.AutoField(primary_key=True))以及其他字段。
  $(document).ready(function(){
      $('#container').highcharts({
          xAxis: {
              categories: {{ classK.attr1 }}
          },
          series: [{
              data: {{ classK.attr2 }}
          }]
      });
  });