【问题标题】:Django 1.10 - Update Web Page with data from Database every X secondsDjango 1.10 - 每 X 秒使用数据库中的数据更新网页
【发布时间】:2018-08-09 12:07:00
【问题描述】:

我需要每隔 X 秒使用数据库中的新信息更新我的网页。这是一个相对较小的应用程序,所以我认为 schedule 可以完成这项工作。

到目前为止我做了什么:

  • 从数据库中获取最新值
  • 传递给模板
  • 遵循计划文档(或者至少我认为我这样做了),但是当我打印到控制台作为测试时似乎没有任何反应。

这是我在views.py中的代码:

from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from lineoee.models import Lineoee3

import threading
import time
import schedule

def job():
        last_oee1 = oee_list[-1]
        print(last_oee1) #test print

def index(request):
        context = {}

        lines = Lineoee3.objects.all().values('oee') 
enter code here
        oee_list = list(Lineoee3.objects.all().values_list('oee', 
flat=True))

        schedule.every(10).seconds.do(job)

        last_oee = oee_list[-1]
        var = "Current OEE is: "

        context = {'lines' : lines, 'var' : var, 'last_oee' : last_oee,}
        return render(request, 'lineoee/index.html',context)

上面的代码运行良好,除了schedule part。没有给出错误。

如何每X 秒打印最后一个oee 值的更新版本?

【问题讨论】:

标签: python django database sqlite


【解决方案1】:

您甚至不需要 Javascript。只需将以下 meta refresh 标签添加到您的模板:

<meta http-equiv="refresh" content="60">

【讨论】:

    【解决方案2】:

    是的,我会在你的模板中留下一个脚本lineoee/index.html

    <script>
        setTimeout(function reload() {location.reload()}, 2000)
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      相关资源
      最近更新 更多