如何解决基于python脚本更改django视图显示内容的理论
嘿,我想基于在后台运行的python脚本更改Webiste上的图像,但我不知道该怎么做。脚本不断运行,以打开和关闭设备,如果设备打开,我想更改视图显示。我该怎么做?我有一个主意,但我不知道这是否好,在数据库中使用布尔值创建表,并在脚本中更新值True / False,并在Django中从数据库读取数据并将其传递给视图,然后使用它。这是个好主意吗?还是有更好的选择?
在这里,我提出了简单的实现,但是我无法从状态模型返回任何对象。 views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import S280119514568Ff,S280000020E3120,S280119514568Ff,S2801195146F4Ff,S280119514D1Eff,S28011951743Bff,S285A1993D983Ff,S285A1993Ed66Ff,S285A1993F99Fff,Sensors,max31856,state
# Create your views here.
def home(request):
context = {
's1':S280119514D1Eff.objects.last(),}
return render(request,'ftemp/home.html',context)
def smoke(request):
return render(request,'ftemp/smoke.html')
def refresh_data(request):
context = {
'termometry': Sensors.objects.all(),'s1':[
S280000020E3120.objects.last(),S280119514568Ff.objects.last(),S2801195146F4Ff.objects.last(),S28011951743Bff.objects.last(),S285A1993D983Ff.objects.last(),S285A1993Ed66Ff.objects.last(),S285A1993F99Fff.objects.last(),S280119514D1Eff.objects.last(),max31856.objects.last(),],'data': S280000020E3120.objects.last().date,'pumps': state.objects.all(),'ftemp/refresh_data.html',context)
我的模板refresh_data.html
<!DOCTYPE html>
<body>
{% for s in s1 %}
<div class="S{{s.sensor.id}}">
<table>
<!-- <td>{{s.sensor.id}}</td> -->
<!-- <td>{{s.sensor.description}}</td> -->
<!-- <td>{{s.sensor.sensor_name}}</td> -->
<td>{{s.temprature}}</td>
<!-- <td>{{s.date}}</td> -->
</table>
</div>
{% endfor %}
<div class="data">Ostatni odczytany pomiar: {{data}} </div>
<div class='pumps'>
Stan pomp
<table>
{{pumps}}
</table>
</div>
</body>
models.py
from django.db import models
from django.utils import timezone
class state(models.Model):
pumpCWU = models.BooleanField(default=True)
pumpCO = models.BooleanField(default=True)
d4o = models.BooleanField(default=True)
d4c = models.BooleanField(default=True)
class Meta:
db_table='state'
python3 manage.py shell的输出
from ftemp.models import state
>>> state.objects.all()
<QuerySet [<state: state object (1)>]>
>>> state.objects.last().pumpCWU
False
我可以看到所有'termometry''数据',但是带有'pumps'state.object.all()我希望我看到该表的queryset,但是我看到的只是空白页,{{pumps}}
应该是。有什么想法吗?
解决方法
如果您不发布代码,则不会得到全面的答案,但是您可以轻松更改向视图内的人显示的视图,例如:
def home(request):
if some.condition:
return redirect('some-other-view')
elif some.other.condition:
return redirect('some-other-view')
return render(request,'myapp/home.html')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。