skip to Main Content

i’m developing a site that uses Django to check cryptocurrency prices.
I want to load a value stored in django sqlite3 database file without refreshing (F5) using ajax. 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.

index.html (this HTML template is static, its value does not change in real time.)

{% 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

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

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

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)

2

Answers


  1. What I would do, would be to create an api that would bring me the (dynamic) value.
    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.

    By the way, I would recommend using CBV instead of FBV.

    Login or Signup to reply.
  2. 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/)

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search