Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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
Django查询数据库并将数据发送到模板_Django_Postgresql_Django Models_Highcharts_Django Views - Fatal编程技术网

Django查询数据库并将数据发送到模板

Django查询数据库并将数据发送到模板,django,postgresql,django-models,highcharts,django-views,Django,Postgresql,Django Models,Highcharts,Django Views,我还是Django/web开发新手,但有Python和PSQL方面的经验 我有一个psql数据库,其中的一个表具有股票投资组合的id、日期和值。该表的标题为absolutedollarvalue 我想将数据发送到模板,然后使用Highcharts.JS将其绘制成图形。据我所知,我需要设置models.py来查询安装在Django中的数据库,将其转换为JSON,然后将其发送到模板进行绘图 有人能告诉我这是否是获取id、日期和值的正确语法,以及如何将json格式的日期和值发送到模板吗 这是适用的模型

我还是Django/web开发新手,但有Python和PSQL方面的经验

我有一个psql数据库,其中的一个表具有股票投资组合的id、日期和值。该表的标题为absolutedollarvalue

我想将数据发送到模板,然后使用Highcharts.JS将其绘制成图形。据我所知,我需要设置models.py来查询安装在Django中的数据库,将其转换为JSON,然后将其发送到模板进行绘图

有人能告诉我这是否是获取id、日期和值的正确语法,以及如何将json格式的日期和值发送到模板吗

这是适用的模型零件:

class Absolutedollarvalue(models.Model):
    cik = models.CharField(max_length=16)
    date = models.DateField()
    value = models.DecimalField(max_digits=65535, decimal_places=65535)

    class Meta:
        managed = False
        db_table = 'absolutedollarvalue'
下面是my views.py代码:

from django.shortcuts import render
from django.http import HttpResponse
from django.db import connection
from toa.models import Absolutedollarvalue as adv
import psycopg2
import json
import datetime

def adv(request):
    adv.objects
    a = adv(id = '1103804')

    return render(request,toa/graphs.html,a)
这是我的模板:

<html>
<head>
  <title> Fund Performance </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>

</head>
<body>
  <div id="chart_container" style="width:100%; height:400px;"></div>
  <script type="text/javascript">
    $(document).ready(function() {
      var absolutedollarval = {
        chart: {
          renderTo: 'chart_panel',
          type: 'time-series',
        },
        legend: {enabled: false},
        title: {text: 'Daily NAV'},
        xAxis: {title:{text:'Date'}},
        yAxis: {title: {text: 'dollar value'}}
      };
      var chartDataUrl = "{% url 'chart_data_json' %}?name = avg_by_day"
        +"&days=14";
      $.getJSON(chartDataUrl,
      function(data){
        absolutedollarval.xAxis.categories=
        absolutedollarval.series[0].name = 
        absolutedollarval.series[0].data = 
        var chart = new Highcharts.Chart(absolutedollarval);
      });
    });
  </script>
</body>
</html>
from django.core import serializers

def adv(request):
    adv.objects
    a = adv(id = '1103804') #Will fetch all the column
    a = adv(id = '1103804').values('columnName') #Will fetch the particular column.
    data = serializers.serialize('json', a)
    context = Context(data)
    return render(request,'toa/graphs.html',context)
    render(request, "foo.html", {'jsonKey': jsonValue}) #For passing individual JSON

将JSON格式的值发送到模板:

<html>
<head>
  <title> Fund Performance </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>

</head>
<body>
  <div id="chart_container" style="width:100%; height:400px;"></div>
  <script type="text/javascript">
    $(document).ready(function() {
      var absolutedollarval = {
        chart: {
          renderTo: 'chart_panel',
          type: 'time-series',
        },
        legend: {enabled: false},
        title: {text: 'Daily NAV'},
        xAxis: {title:{text:'Date'}},
        yAxis: {title: {text: 'dollar value'}}
      };
      var chartDataUrl = "{% url 'chart_data_json' %}?name = avg_by_day"
        +"&days=14";
      $.getJSON(chartDataUrl,
      function(data){
        absolutedollarval.xAxis.categories=
        absolutedollarval.series[0].name = 
        absolutedollarval.series[0].data = 
        var chart = new Highcharts.Chart(absolutedollarval);
      });
    });
  </script>
</body>
</html>
from django.core import serializers

def adv(request):
    adv.objects
    a = adv(id = '1103804') #Will fetch all the column
    a = adv(id = '1103804').values('columnName') #Will fetch the particular column.
    data = serializers.serialize('json', a)
    context = Context(data)
    return render(request,'toa/graphs.html',context)
    render(request, "foo.html", {'jsonKey': jsonValue}) #For passing individual JSON

你的
adv
函数看起来像废话。您应该阅读django文档,了解如何编写视图函数并传递要在模板中呈现的变量。我使用django文档检索对象,他们给出了一个示例:>>>Blog.objects>>>b=Blog(name='Foo',tagline='Bar')>>>b.objects我认为对象a应该给我所有具有该id的数据行。我只想将日期和值转换为json,然后将其发送到HighChart Django文档肯定没有给出如何查询的示例。请阅读教程。Do
adv.objects.filter(cik='1103804')。values('Column1','Column2')
,您将获得字典列表。是否有单独发送数据的方法?像date列和相应的value列一样?这样在highcharts中就很容易有x轴和y轴的单独数据?@user3628240,您可以像这样传递它
render(请求,“foo.html”,{jsonKey':jsonValue})
Ok,谢谢!你能告诉我你将如何获得adv的各个栏目吗?所以对象a基本上应该是具有id、日期和值的元组组。我想传递日期和相应的值。我是否需要执行另一个查询?另外,adv.objects.filter(id=“1103804”)与您在上面编写它的方式是否有区别?谢谢你的帮助