如果我问一些愚蠢的事情,请原谅我,我是编码行业的新手
我一直在Django项目中创建仪表板,以显示对水培温室的案例监视。
所以我创建了两个模型;
class tank_system(models.Model):
PH = models.DecimalField(default=7.5,max_digits=3, decimal_places=1)
EC = models.DecimalField(default=13.3,max_digits=3, decimal_places=1)
Winlet = models.DecimalField(default=20.1, max_digits=3, decimal_places=1)
Woutlet = models.DecimalField(default=20.3,max_digits=3, decimal_places=1)
WaterLevel = models.IntegerField(default=500)
TempWater = models.IntegerField(default=25)
tanks = models.IntegerField(default=1)
datetime = models.DateTimeField(default=timezone.now())
class ambient (models.Model):
TempRoom = models.IntegerField(default=25)
CO2 = models.DecimalField(default=20.0, max_digits=3, decimal_places=1)
O2 = models.DecimalField(default=20.0,max_digits=3, decimal_places=1)
Humdity = models.IntegerField(default=25)
Room = models.IntegerField(default=1)
datetime = models.DateTimeField(default=timezone.now())
然后将两个模型的数据渲染到一个模板中
这是我的观点
from django.shortcuts import render, get_object_or_404
from CGI.models import tank_system, ambient
def index(request):
tank = tank_system.objects.all()
room = ambient.objects.all()
return render(request, 'CGI/Pages/DashBoard.html', {'tank': tank},{'room': room})
当我尝试此操作时,我仅从1表获得数据
这是我的html模板;
{% extends "base.html" %}
{% block content %}
{%load staticfiles%}
{% load static %}
<div class = "containor">
<div class ="row" id = "row1">
<div class ="col-sm-6" style="background-color:">
<h2>Camera</h2>
</div>
<div class ="col-sm-6" style="background-color:">
<h2>Ambinet</h2>
</div>
</div>
<div class = "containor">
<div class="row" id="row2">
<div class ="col-sm-6" id="Cam1" style="background-color:">
<div class="containor">
<video id="livestream" width="550" height="350" autoplay></video>
<canvas class = "my-4 chartjs-render-monitor" id="live_vid" height="1"></canvas>
<script src="{% static 'FrounterWeb/JS-code/Stream.JS' %}" ></script>
</div>
</div>
<div class ="col-sm-2" id="Humdity" style="background-color:">
<div class="containor">
<center>
<picture>
<img src="{% static 'FrounterWeb/img/Humidty_icon.png' %}" alt="RoomTemp">
</picture>
<br>
<h1>
{% for ambient in room %}
{% if forloop.first %}
{{ ambient.Humdity }}
{% endif %}
{% endfor %}
%</h1>
</center>
</div>
</div>
<div class ="col-sm-2" id="Roomtemp" style="background-color:">
<div class="containor">
<center>
<picture>
<img src="{% static 'FrounterWeb/img/RoomTemp.png' %}" alt="RoomTemp">
</picture>
<br>
<h1>
{% for ambient in room %}
{% if forloop.first %}
{{ ambient.TempRoom }}
{% endif %}
{% endfor %}
C</h1>
</center>
</div>
</div>
<div class ="col-sm-2" id="co2" style="background-color:">
<div class="containor">
<center>
<picture>
<img src="{% static 'FrounterWeb/img/co2_icon.png' %}" alt="co2">
</picture>
<br>
<h1>
{% for ambient in room %}
{% if forloop.first %}
{{ ambient.CO2 }}
{% endif %}
{% endfor %}
</h1>
</center>
</div>
</div>
</div>
</div>
<div class = "containor" >
<div class="row" id = "row3" >
<div class ="col" style="background-color:">
<center>
<h2>Water tank</h2>
</center>
</div>
</div>
</div>
</div>
<div class = "containor">
<div class="row" id ="row4" >
<div class ="col-sm-2">
<div class="containor">
<center>
<picture>
<img src="{% static 'FrounterWeb/img/pHicon.png' %}" alt="co2">
</picture>
<br>
<h1>
{% for tank_system in tank %}
{% if forloop.first %}
{{ tank_system.PH }}
{% endif %}
{% endfor %}
</h1>
</center>
</div>
</div>
<div class ="col-sm-2">
<div class="containor">
<center>
<picture>
<img src="{% static 'FrounterWeb/img/EC-icon.png' %}" alt="co2">
</picture>
<br>
<h1>
{% for tank_system in tank %}
{% if forloop.first %}
{{ tank_system.EC }}
{% endif %}
{% endfor %}
%</h1>
</center>
</div>
</div>
<div class ="col-sm-2">
<div class="containor">
<center>
<picture>
<img src="{% static 'FrounterWeb/img/WaterTemp.png' %}" alt="co2">
</picture>
<br>
<h1>
{% for tank_system in tank %}
{% if forloop.first %}
{{ tank_system.TempWater }}
{% endif %}
{% endfor %}
C</h1>
</center>
</div>
</div>
<div class ="col-sm-2">
<div class="containor">
<center>
<picture>
<img src="{% static 'FrounterWeb/img/Flow-icon.png' %}" alt="co2">
</picture>
<br>
<h1>
{% for tank_system in tank %}
{% if forloop.first %}
{{ tank_system.Winlet }}
{% endif %}
{% endfor %}
ml/hr</h1>
</center>
</div>
</div>
<div class ="col-sm-2">
<div class="containor">
<center>
<picture>
<img src="{% static 'FrounterWeb/img/Flow-out-icon.png' %}" alt="co2">
</picture>
<br>
<h1>
{% for tank_system in tank %}
{% if forloop.first %}
{{ tank_system.Woutlet }}
{% endif %}
{% endfor %}
ml/hr</h1>
</center>
</div>
</div>
<div class ="col-sm-2">
<div class="containor">
<center>
<picture>
<img src="{% static 'FrounterWeb/img/waterlevel-icon.png' %}" alt="Waterlevel">
</picture>
<br>
<h1>
{% for tank_system in tank %}
{% if forloop.first %}
{{ tank_system.tanks }}
{% endif %}
{% endfor %}
L</h1>
</center>
</div>
</div>
</div>
</div>
<div class = "containor">
<div class="row" id="row5" >
<div class ="col-sm-6" >
<h1>Water data</h1>
</div>
<div class ="col-sm-6" >
<h1>Room data</h1>
</div>
</div>
</div>
<div class = "containor">
<div class="row" id="row6" >
<div class ="col-sm-6" >
<div class="float-sm-none" id = Waterchart>
</div>
</div>
<div class ="col-sm-6" >
<div class="float-sm-none" id = Roomchart>
</div>
</div>
</div>
</div>
如果可以帮助我,真的很感激
答案 0 :(得分:2)
您在渲染中有4个参数,其中2个是上下文。它只需要带有上下文的一个字典,或者您可以创建一个上下文字典变量,并将其作为参数传递给render。
尝试一下:
from django.shortcuts import render, get_object_or_404
from CGI.models import tank_system, ambient
def index(request):
tank = tank_system.objects.all()
room = ambient.objects.all()
return render(request, 'CGI/Pages/DashBoard.html', {'tank': tank,'room': room})
OR
from django.shortcuts import render, get_object_or_404
from CGI.models import tank_system, ambient
def index(request):
tank = tank_system.objects.all()
room = ambient.objects.all()
context = {
'tank': tank,
'room': room
}
return render(request, 'CGI/Pages/DashBoard.html',context)
答案 1 :(得分:2)
您可以使用两种不同的方式传递数据
def index(request):
tank = tank_system.objects.all()
room = ambient.objects.all()
return render(request, 'CGI/Pages/DashBoard.html', {'tank': tank,'room': room})
or
return render(request, 'CGI/Pages/DashBoard.html', locals())
locals() stands for get all local variable/objects and pass to the html as context
答案 2 :(得分:1)
在索引方法中,更改为:
def index(request):
tank = tank_system.objects.all()
room = ambient.objects.all()
return render(request, 'CGI/Pages/DashBoard.html', {'tank': tank, 'room': room})