Get latest Bitcoin and other crypto-currencies rates using python Django

Everybody is investing in bitcoins. James Howells is trying to dig a landfill site to get 7500 bitcoins that were dumped there in 2013.

To be a good investor, it is necessary that you keep track of ups and downs in the market. There are multiple platforms where you can track the price of bitcoin. But for a python programmer that is no fun. Being a python programmer we will develop our own project where we can get latest bitcoin and other crypto-currency prices.

Let’s start.

Virtual environment setup:

It is always recommended to use virtual environment for all your python and Django projects.

Create a virtual environment using python3 using below command.

Now activate the virtual environment.

Install the latest Django version and other required libraries. For now only requests package is required. We will add other packages later if required.

This will install Django 2.0 and requests package along with some other package. You can verify the same by running command  pip freeze .

Creating new Django Project:

Once virtual environment has been setup and activated, create a new django project.

Go to crypto project directory and list the files.

python django project bitcoin prices

Since we are working on Django 2.0 , we need to take care of few things which we will highlight as we progress.

Now create a new app ‘bitcoin’.

Add this app to the list of installed apps in settings.py  file.

Project Setup:

URLs

Create urls.py  file in new app bitcoin.

Django 2.0 Note: Adding app_name  in urls.py  is require now, otherwise you will get the below error.

 

Include urls of bitcoin app in project’s urls.py  file.

 

Teamplate

Create a directory templates/bitcoin  in bitcoin app. Inside this directory create a new html file, index.html . You can leave this file empty as of now or to make sure things are working fine, put some text there.

 

Views

Create a new function index in views.py file. For now this function will only render the index.html  created in previous step. We will add more functionality in coming steps.

 

Verifying basic setup:

Once above steps are completed, run the django server.

Go to localhost:8000  and you can see the text which you putted in index.html  file.

 

Getting bitcoin prices:

We will use the coinmarktecap api to fetch the latest data.

In views.py  file add a new function to get bitcoin data. This function will call the api url and get the currency data. Data returned is Json string. Convert it to Json Object.

Make a call to get_crypto_data  in index function and return the rendered response.

Complete views.py  file :

 

Rendering Data in Template:

Render the coins data row wise using a for loop in template as shown in html code below.

We are using bootstrap.css  for styling.

Now if you go to localhost:8000  you can see the details as in below image.

bitcoin prices in python django

Complete code is available on Github. Download and setup the project on your machine. Experiment with it and let us know in case of any query.

 

References:
[1] https://coinmarketcap.com/api/
[2] Django 2.0 official documentation

 

(Visited 1,141 times, 1 visits today)

You must read this :

Leave a Reply

Your email address will not be published. Required fields are marked *