DEV Community

Cover image for Corona Tracker | Django Project |
Madhuban Khatri
Madhuban Khatri

Posted on

Corona Tracker | Django Project |

Hello Friends,
Do you also want to make your own website like Corona Tracker so that you can get updates of Corona sitting at home?
In this blog you will get the source code of Corona Tracker Project, with the help of which you can create your tracker.

SOURCE CODE

views.py

from django.shortcuts import render
from django.http import HttpResponse

from bs4 import BeautifulSoup
import requests

url = 'https://www.mygov.in/corona-data/covid19-statewise-status/'

r = requests.get(url)
data = r.content
soup = BeautifulSoup(data, 'html.parser')


# Create your views here.
def index(request):
    active_cases = soup.find_all('div', {"class": "field-name-field-total-active-case"})
    cured_cases = soup.find_all('div', {"class": "field-name-field-total-cured-discharged"})
    death_cases = soup.find_all('div', {"class": "field-name-field-total-death-case"})

    state_lis = []
    confirmed = []
    cured = []
    deaths = []

    state_class = soup.find_all('div', {"class": "field-name-field-select-state"})
    confirmed_class = soup.find_all('div', {"class": "field-name-field-total-confirmed-indians"})
    cured_class = soup.find_all('div', {"class": "field-name-field-cured"})
    death_class = soup.find_all('div', {"class": "field-name-field-deaths"})

    for state in state_class:
        state_lis.append(state.text[12:])
    for confirm in confirmed_class:
        confirmed.append(confirm.text[17:])
    for cure in cured_class:
        cured.append(cure.text[28:])        
    for death in death_class:
        deaths.append(death.text[7:])

    params = {"active_cases": active_cases[0].text[13:],
              "cured_cases": cured_cases[0].text[18:],
              "death_cases": death_cases[0].text[7:],
              "states": state_lis,
              "confirm": confirmed,"deaths": deaths, "cured": cured}
    return render(request, 'index.html', params)

Enter fullscreen mode Exit fullscreen mode

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index')
]
Enter fullscreen mode Exit fullscreen mode

index.html

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">

    <title>Corona Tracker</title>
  </head>
  <body>
    <div class="container btn-primary w-50">
      <h2 style="transform: translate(30%, 0%)">Simple Corona Tracker</h2>
    </div>
    <div class="container my-2 w-75">
      <div class="row">
        <div class="col">
          <h5>Active Cases: <span style="color: blue;">{{active_cases}}</span></h2>
        </div>
        <div class="col">
          <h5>Cured Cases: <span style="color: green;">{{cured_cases}}</span></h2>
        </div>
        <div class="col">
          <h5>Death Cases: <span style="color: red;">{{death_cases}}</span></h2>
        </div>
      </div>
    </div>


    <div class="container border w-25" style="margin-left: 120px; display: inline-block;">
      <h4>STATES</h4>
      {% for s in states %}
        <h6>{{s | truncatechars:27}}</h6>
        <hr>
      {% endfor %}
    </div>

    <div class="container border w-25" style="display: inline-block;">
      <h4>TOTAL CONFRIMED</h4>
      {% for c in confirm %}
        <h6>{{c}}</h6>
        <hr>
      {% endfor %}
    </div>

    <div class="container border w-25" style="display: inline-block;">
      <h4>CURED</h4>
      {% for cr in cured %}
        <h6>{{cr}}</h6>
        <hr>
      {% endfor %}
    </div>

    <div class="container border" style=" display: inline-block; width: 200px;">
      <h4>DEATHS</h4>
      {% for d in deaths %}
        <h6>{{d}}</h6>
        <hr>
      {% endfor %}
    </div>




    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)