Consuming API

In the last post we created some API’s for our best ever  F1 website, now is the time to consume them.

I will be dividing this topic into 2 posts, In this post will be discussing about pulling the data from the F1 website and displaying on my web-page. In the next post I will discuss on how to pull some user specific data. Lets begin.

Before we start lemme set a background of this application which is going to consume the API. The application is F1 driver portfolio. A driver will log into the website, and he will see all the races he has been a part of, he can share memories of his best races on this website, update information and do other fun things. These are some of the features of F1 driver portfolio appliaction.

So lets begin creating this app with django-admin:

~ django-admin startproject F1Portfolio

~ cd F1Portfolio

~ python manage.py startapp driverportfolio

 

The api endpoint of my F1 APPLICATION is ‘http://192.168.0.102:8000/laps/’ , in the previous post I have discussed about the requests module, requests.get() where get is an HTTP verb –  suppose to get data. The data is stored in response variable, python has a inbuilt json decoder which you can see in the next line laps = reponse.json() . laps has all the parsed data.

The views look something like this.

# -*- coding: utf-8 -*-

from django.shortcuts import render
import requests
# Create your views here.

def home(request):
""" This function based view will return the 
json data which will be displayed on our UI"""

    response = requests.get('http://192.168.0.102:8000/laps/')
    laps = response.json()

    return render(request,'home.html',{'laps':laps})

Below is data that we want to populate in our website. ‘laps’  is the variable that will hold the data, you can see that variable in the views.py.

api

 

Once the data is fetch I want to display it on my template. The template is “home.html” looks like this.

{% extends "base.html" %}
{% block content %}
  <h2>F1 API</h2>
  <table class="table">
            <tr>
                <th>Car Model</th>
                <th> Grand pix</th>
                <th> Driver Name</th>
                <th> Time Taken</th>
            </tr>
            {% for l in laps %}
            <tr>
                <td> {{ l.car_model }}</td>
                <td> {{ l.grand_prix }}</td>
                <td> {{ l.driver_name }}</td>
                <td> {{ l.time_taken }}</td>
            </tr>
            {% endfor %}
        </table>


{% endblock %}

Towards the end will configure the url for the homepage.

urls.py

#!/usr/bin/env python
from django.conf.urls import url,include
from django.contrib import admin
from . import views

app_name ='driver'

urlpatterns = [
    url(r'^driver/$', views.home, name='index'),
    ]

 

Running the localhost:8000 I should be able to see all the laps data in my website. The output looks something like this.

Untitled

This is it for this post. Stay tune for what is coming next is super interesting. Code is available in Github.

Happy Coding.

 

One thought on “Consuming API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s