Django Python Dynamic Google Line Charts Example

Published On: 07/07/2022 | Category: Django Python

Hi Dev,

In this tutorial we will go over the demonstration of pie chart example using google chart in django python. We will use django dynamic google pie charts example. step by step explain django python dynamic google pie charts example from scratch. Here you will learn django python google pie chart example tutorial.

Here i explained simply step by step example of here you will learn django python google pie chart example tutorial.

Step 1 : Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2 : Create a App
python3 startapp chart
Step 3 : Update

In this step we require to do two things in our file, One is to change the path of template look up directory. Second one is to configure our media folder. Add the below lines to your file:

Next, you need to add it in the file as follows:

import os

Step 4 : Database Setup

Next step, we will modify the file and update the database settings to configure the mydb database:
    'default': {  
        'ENGINE': 'django.db.backends.mysql',  
        'NAME': 'example',  
Step 5: Create a Model

In this step we will require the database model for storing click and visitors data.Open the chart/ file and add the following code:

from django.db import models

class Visitor(models.Model):
    click = models.IntegerField()
    viewer = models.IntegerField()

After creating these model, you need to create migrations using the following command:

Step 6 : Create a Migrations
python makemigrations

After successfully run the above command go to the chart/migrations/

# Generated by Django 2.1 on 2022-07-04 09:35

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0002_user'),

    operations = [
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('click', models.IntegerField()),
                ('viewer', models.IntegerField()),

Next, you need to migrate your database using the following command:

python migrate
Step 7 : Creating the Views

In this step, we need to create the views for performing fetch record to the database.Open the chart/ file and add:

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from .models import Visitor
import json

# Create your views here.

def googleChart(request):

    #h_var : The title for horizontal axis
    h_var = 'Click'

    #v_var : The title for horizontal axis
    v_var = 'Visitors'

    data = [[h_var,v_var]]
    visitors = Visitor.objects.all()

    for visitor in visitors:

    h_var_JSON = json.dumps(h_var)

    v_var_JSON = json.dumps(v_var)

    modified_data = json.dumps(data)

    return render(request,"charts.html",{'values':modified_data,\
Step 8 : Creating the Templates

Next, open the chart/templates/charts.html file and the add:

    <!--Load the AJAX API-->
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
      // Load the Visualization API and the corechart package.
      google.charts.load('current', {'packages':['corechart']});
      // Set a callback to run when the Google Visualization API is loaded.

      function drawChart() {
        //{{values|safe}} : list of list containing points for the chart
        var data = google.visualization.arrayToDataTable({{values|safe}});
        // Set chart options
        var options = {
          title: {{h_title|safe}} +' vs. '+ {{v_title|safe}},
          hAxis: {title: {{h_title|safe}}},
          vAxis: {title: {{v_title|safe}}},
          legend: { position: 'bottom' }

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    <div class="container" style="margin:40px;">
        <div class="row">
            <div class="col-md-12" style="text-align: center;">
                <h2>Django Python Google Chart Example Tutorial -</h2>
                <div id="chart_div" style="width: 900px; height: 500px;display: inline-block;"></div>
Step 9 : Creating URLs

In this section, we’ll create the urls to access our CRUD views.Go to the chart/ file and update it as follows:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.googleChart),

Next, we will require the modify the your root preoject folder lets update the file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('chart.urls')),
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/ address with a web browser.

I Hope It will help you....