2

I need to update my web page every X seconds with new information from the database. This is a relatively small application so I thought schedule would do the job.

What I have done so far:

  • Getting latest value from database
  • Passing on to template
  • Followed schedule docs (or at least I think I did) but nothing seems to happen when I print to console just as a test.

Here is my code in 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)

The code above works well except for the schedule part. No errors are given.

How do I print an updated version of the last oee value every X seconds?

fcp
  • 89
  • 9
  • 2
    no need to use `schedule` for this, you can reload your page with javascript https://stackoverflow.com/questions/2787679/how-to-reload-page-every-5-second – Mohit Solanki Aug 09 '18 at 12:12

2 Answers2

3

You don't even need Javascript for that. Just add the following meta refresh tag to your template:

<meta http-equiv="refresh" content="60">
Selcuk
  • 57,004
  • 12
  • 102
  • 110
1

Yes, I would leave a script in your template lineoee/index.html:

<script>
    setTimeout(function reload() {location.reload()}, 2000)
</script>
Boris
  • 490
  • 5
  • 11