[英]Django. How i can run my function every 5 second and update the page
I have a Django app.我有一个 Django 应用程序。 I want that every 5 seconds, the script will update DB dates and reload my page.我希望每 5 秒,脚本将更新数据库日期并重新加载我的页面。 I tried with a simple: while()
, but this stopped all code.我尝试了一个简单的方法: while()
,但这停止了所有代码。 I tried with async function, but nothing is working我尝试使用异步 function,但没有任何效果
My code: `from django.shortcuts import render
from django.http import HttpResponse
from django.db import models
from main.models import Prices
import requests
from bs4 import BeautifulSoup
import time
# Create your views here.
timeout = 5
def parseSite():
Prices.objects.all().delete()
URL = "https://www.coindesk.com/price/bitcoin"
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
cont = soup.find(class_="coin-info").find_all(class_="coin-info-block")
final_price = cont[0].find(class_="price-large").text
bitoc = Prices(curency_type = "bitcoin", curency_price = final_price)
bitoc.save()
parseSite()
def dates():
bitoc2 = Prices.objects.get(curency_type= "bitcoin")
return bitoc2
def index(request):
data = dates()
return render(request, 'main/index.html', {'data': data})
`
if you really need to refresh the page, so usin html Meta tag could be a solution.如果您确实需要刷新页面,那么使用 html 元标记可能是一个解决方案。 <meta http-equiv="refresh" content="5">
and 5
is in seconds. <meta http-equiv="refresh" content="5">
和5
以秒为单位。 but if you need realtime changes in a part of a page, as Erfan mentioned use websocket & djnago-channels,...
但是如果您需要在页面的一部分中进行实时更改,正如Erfan提到的使用websocket & djnago-channels,...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.