簡體   English   中英

Django。 我如何每 5 秒運行一次 function 並更新頁面

[英]Django. How i can run my function every 5 second and update the page

我有一個 Django 應用程序。 我希望每 5 秒,腳本將更新數據庫日期並重新加載我的頁面。 我嘗試了一個簡單的方法: while() ,但這停止了所有代碼。 我嘗試使用異步 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})
`

如果您確實需要刷新頁面,那么使用 html 元標記可能是一個解決方案。 <meta http-equiv="refresh" content="5">5以秒為單位。 但是如果您需要在頁面的一部分中進行實時更改,正如Erfan提到的使用websocket & djnago-channels,...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM