使用Django和cryptocompare在表中显示加密价格

2024-06-16 15:56:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在django制作一个简单的网页,在那里我可以显示一些加密货币的价格。用户在数据库中存储加密的名称及其初始价格。然后我想用股票代码和从数据库中获取的初始价格以及使用cryptocompare获取的每个加密的当前价格填充一个表

问题就从这里开始:“当前价格”的值是使用cryptocompare获取的,但是如何在表中以二进制方式显示每个加密的当前价格的值呢?也许这个问题很容易解决,但我真的是新手,我被卡住了,提前谢谢! 如果你需要更多信息,请问我

这就是索引中的表的外观。我需要在最后一列填上加密软件的每日价格 enter image description here

models.py

from django.db import models

# Create your models here.
class crypto(models.Model):
    ticker = models.CharField(max_length=200)
    initial_price = models.FloatField()

    def __str__(self):
        return self.ticker

views.py

from .models import crypto

def index_view(request):
    chart = FuncAnimation(plt.gcf(), animate, interval=1000)
    ticker_list = crypto.objects.all()
    
    return render(request, 'cryptochart/index.html', {'chart': chart, 'ticker_list': ticker_list})

最后,在一个名为utils.py的文件中,我编写了获取价格的函数

import cryptocompare
#get price of cryptocurrency
def get_crypto_price(cryptocurrency,currency):
    return cryptocompare.get_price(cryptocurrency,currency=currency)[cryptocurrency][currency]

#get fullname of the cryptocurrency
def get_crypto_name(cryptocurrency):
    return cryptocompare.get_coin_list()[cryptocurrency]['FullName'] 

index.html

<table style="width: 100%;">
        <tr>
            <th>Ticker</th>
            <th>Prezzo Iniziale</th>
            <th>Prezzo Giornaliero</th>                
        </tr>
        
        {% if ticker_list %}
        {% for ticker in ticker_list%}
        <tr>
            <td>{{ticker.ticker}}</td>
            <td>{{ticker.initial_price}}</td>
            <td>{{???}}</td>
            
        </tr>
        {%endfor%}
        {% endif %}
    </table>

Tags: getreturnmodelsdef价格cryptopricetr