简体   繁体   English

重新加载页面而不用 django 和 ajax 刷新

[英]Reload the page without refreshing with django and ajax

i'm developing a site that uses Django to check cryptocurrency prices.我正在开发一个使用 Django 来检查加密货币价格的网站。 I want to load a value stored in django sqlite3 database file without refreshing (F5) using ajax.我想加载存储在 django sqlite3 数据库文件中的值而不使用 ajax 刷新(F5)。 What should I do?我该怎么办?

(However, if the user manually refreshes the page, the value will change. This is because the value is loaded from the database.) (但是,如果用户手动刷新页面,该值会发生变化。这是因为该值是从数据库加载的。)

Cryptocurrency prices and other information are automatically collected by the crawler and stored in real time in db.sqlite3 in your Django project.加密货币价格和其他信息由爬虫自动收集并实时存储在您的 Django 项目中的 db.sqlite3 中。

index.html (this HTML template is static, its value does not change in real time.) index.html (此 HTML 模板是静态的,其值不会实时更改。)

{% extends 'exchange/layout.html' %}
{% load humanize %}
{% block title %}XCoin{% endblock %}
{% block body %}
<table class="table table-condensed">
 <thead>
   <tr>
      <th>#</th>
      <th>Name</th>
      <th>Price</th>
      <th>Market Cap</th>
      <th>Volume (24h)</th>
      <th>Circulating Supply</th>
   </tr>
 </thead>
 <tbody>
   {% for coin in coins %}
   <tr>
      <td>{{coin.ranking}}</td>
      <td>[ {{coin.abbreviation}} ]  {{coin.name}}</td>
      <td>{{coin.price|intcomma}}$</td>
      <td>{{coin.marketcap|intcomma}}$</td>
      <td>{{coin.volume|intcomma}}$</td>
      <td>{{coin.circulatingsupply|intcomma}} {{coin.abbreviation}}</td>
   </tr>
   {% endfor %}
 </tbody> 

</table>
{% endblock %}

urls.py网址.py

from django.contrib import admin
from django.urls import path
from . import views

app_name = 'exchange'
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='home'),
]

models.py模型.py

from django.db import models

class Coin(models.Model):
    ranking = models.IntegerField() # coin ranking
    abbreviation = models.CharField(max_length=10) # coin symbol
    name = models.CharField(max_length=20) # coin name
    price = models.FloatField() # coin price ($)
    marketcap = models.IntegerField() # coin marketcap ($)
    volume = models.IntegerField() # coin volume (24h)
    circulatingsupply = models.IntegerField() # coin supply

    def __str__(self):
        return self.name

views.py视图.py

from .models import Coin
from django.shortcuts import render
from django.shortcuts import HttpResponse

def index(request):
    coin = Coin.objects.all()
    context = {'coins':coin}
    return render(request, 'exchange/index.html', context)

What I would do, would be to create an api that would bring me the (dynamic) value.我会做的是创建一个可以为我带来(动态)价值的 api。 then by means of react/angular or whatever suits you best create an event that shows you the value on screen (dynamically too), but not render that value from the context in view, render it from the same JS.然后通过 react/angular 或任何最适合您的方式创建一个事件,在屏幕上显示值(也是动态的),但不从上下文中呈现该值,而是从同一个 JS 呈现它。

By the way, I would recommend using CBV instead of FBV.顺便说一下,我建议使用 CBV 而不是 FBV。

the simplest solution is to create a view that contains the data and a view that acts as a frame.最简单的解决方案是创建一个包含数据的视图和一个充当框架的视图。 Set a timer in the frame view and then use jQuery.load() ( https://api.jquery.com/load/ )在框架视图中设置一个计时器,然后使用 jQuery.load() ( https://api.jquery.com/load/ )

an exmaple will be:一个例子是:

var l_url = '/path/to/data_view'
var l_html = $('div#test-load');
l_html.load(l_url,function(data,status,jqXGR){
   //add some additional processing here if you want to process the returned data
   //if your data_view is designed correclty you shouldn't need it
}

using an api and puling the data from the db(as suggested in the posts above) is however the better option.然而,使用 api 并从数据库中提取数据(如上面的帖子中所建议的)是更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM