python - Django 1.10 - 每 X 秒使用数据库中的数据更新网页

标签 python django database sqlite

我需要每 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)

除了计划部分之外,上面的代码运行良好。没有给出任何错误。

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

最佳答案

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

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

关于python - Django 1.10 - 每 X 秒使用数据库中的数据更新网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51766602/

相关文章:

python - 如何操作 pandas 数据框列,其中列名是带有后缀的日期时间字符串?

Python键绑定(bind)/捕获

python - 收到意外的关键字参数 'pk'

mysql - 不同数据库中字符串的压缩

python - 如何以与 Python 和 Pyenv 类似的方式从 shell 管理多个版本的 R

python - 修改玩具 scikit-learn gridsearchCV 示例时收到警告 "UserWarning: One or more of the test scores are non-finite"

python - Django:合并来自表单和数据库的文本

Django 社交授权 :Get email from linkedin, 推特和 Facebook

database - 是否有任何支持多线程的本地数据库?

java - 在 Hibernate 中创建更新返回查询