How To Create Charts In Django


The Python programming language was becoming increasingly popular in the development of web-based applications. In an attempt to leverage this and help speedily deploy secure and maintainable web applications, the Django web framework was developed.

The open-source web framework of Django, which is based entirely on Python, follows the model-view-template (MVT) architectural pattern. The framework focuses on reusing components created to help with rapid development. GIven that it is Python-based, the framework is highly portable and can run on any platform and has support for several client-side frameworks, templating engines, and databases.

In this tutorial, we will look at how we can create charts using the Django framework. We will use the FusionCharts Javascript library to create the charts.


To create a chart in a web app developed using Django, you will need the following to be downloaded and installed on your system:

    Django Framework – Download Link
    FusionCharts Library – Download Link
    FusionCharts Django wrapper – Download Link

Note: You will require Python to be installed to start with Django and will also require working knowledge of Python to execute the code.

Creating charts using the Django Framework

Step 1: Creating the project

Run the following command to create a the myproject directory in the current directory.

django-admin startproject myproject
You can use the cd command on the command line interface to navigate to the required directory.

Step 2: Adding the dependencies

Next, we will add the FusionCharts javascript files required to render the charts in the web application.

  1. Create a template folder inside the myproject directory.
  2. Inside the template folder, create another folder named static.
  3. Copy all the javascript files extracted from the FusionCharts library into this folder.
  4. To map the javascript files, create a HTML file, index.html, in the template folder. This file will have the path to each javascript file and also the output element.
  5. After including the paths of all the dependencies required, we need to update the static files by running the following command
    python collectstatic
  6. In the file, update the location of the templates
    'DIRS': ['fc_column2d/templates'],

Step 3: Creating the view page

The view page contains the chart constructors, attributes as well as the datasource required to render the chart.

  1. Add the code given below to the file.

Step 4: Setting up the configuration file

  1. Add the following code snippet to the file, to automatically set the URL to render the chart.
  2. To update the STATICFILES_DIRS object, include it in the file.

Step 5: Rendering the chart

With all the code in place, it is now time to render the chart.

  1. Run the following command on the shell interface
    python runserver

If you’ve followed the steps closely, your output should like the chart shown in the image below:

If you see any errors in your code, you can download the complete source code of the sample project we have created for this tutorial.
The article was first published here.

About the author: Software programmer, coffee snob, evangelist but a nerd by soul.

Leave a Comment