ChartJs Integration with Django

Amit Hadole   21 January,2020  

ChartJs integration with Django

Step 1: Create Django project

1)Following is the command for creating django project:

$ django-admin startproject <Project Name>

Step 2: Create Application

1)Using following command we can create application:

$ django-admin startapp <Application Name>

2)When we create django application, its compulsory to add that application name in 'INSTALLED_APPS' section in file which is present in our project folder.


Step 3: Running the Test Server

Let’s verify your Django project works, use following command:

$ python runserver

You will see the following output on the terminal:


Now that the server’s running, visit with your Web browser. You’ll see a “Congratulations!” page, with a rocket taking off. It worked!


Step 4: Create Template

Create template folder in application folder

Django doesn’t know the path of template. To set the template path open file in our project folder and paste following path into the DIRS into the template section.


os.path.join(BASE_DIR, 'db.sqlite3'),


Step 5: Create model

When we create project, we need to create simple model in file of django project which contain data which is shown in our chart.

In the model first we need to create class CityPopulation which is inherit from models.Model

So, the first field or column in our database is 'city' which is CharField. The maximum length of that field we can set using 'max_length'

after that we add another field that is 'population'. This is integer type of field for that we use IntegerField.


after creating model we make migrations for that we use following command

$ python makemigrations.


and then migrate the changes using migrate command

$ python migrate


Step 6: Register model in

After creating model, register that model in file in the project folder, as shown in following picture.


Step 7: Create Function(view)

We can write your function or code part in file in application folder.


Step 8: Create Template

create chart.html file in template folder.

In that file type exclamation mark (!) and press Enter key. It gives basic html structure.

You can set your title using <title> tag.


Step 9: Set Home URL

To display home page, we need to set URL in ‘’ file in the project folder.


Chart.js is the JavaScript library which is used to show different 8 types of charts

To add that ChartJs CDN follow the following steps:

1)Just open and click to the 'Get started'. And then open installation page.


2)Click on '' link and copy CDN that is '' and paste it on chart.html file under the <head> tag section using <script> tag.


3)For bootstrap CDN open '' and copy the link below the Complete CSS that is '' and paste it in <link> tag under the <head> section.


We have added charjs, now in <body> section we need to create <div> with the class 'container'.

and in that <div> we create element and give id to that. We can also set the height and width in that <canvas>.


After that everything we will be done in java script that’s why we can add <script> tag and write in between that tag.

In that we need to create variable for our chart and get element id that is 'myChart'.


Then create variable 'massPopChart' and we want set up new object in that. We set parameter that is 'myChart' and also set second parameter that is an object and this is where all the properties, data, etc exist.

type: Type of chart include bar, horizontalBar, pie, line, doughnut, radar and polar Area.

data: In the data there are labels and datasets.


Screen will look like after set  type, data and options.


Once you run python server, you will see the following screen −


Then add labels and dataset in data.

In this project we can show the city and their population thats why we add cities and their population data in database using Django administration.


To show cities in chart, add cities dynamically in 'labels' and for population, add it dynamically in 'data' as shown in the following screen.


We can add background color in the chart, for that we use 'backgroundColor'


After adding background color chart will look like


we can also set the border width, border color, hover border width and hover border color as shown in screen.


After adding borderColor, hoverBorderWidth and hoverBorderColor


For set font size, font family and font color globally add following options


In option we can set

1) title: {

            display: true,

            text: 'Population of cities in India'




2) legend: {

            Position: 'right',

            Display: true,

            labels: {

                    fontColor: '#000'




3) padding: {








4) tooltips: {




If you want to see another chart then change chart type


After changing chart type ‘bar’ to ‘pie’