Matplotlib into Django Template

Madhav Kothari
3 min readNov 29, 2019

--

Django+Matplotlib

How to show matplotlib graph into Django template —

Requirements -

Django — 2.2.5

matplotlib — 3.1.2

numpy — 1.17.4

How to install the above libraries -

pip install django==2.2.5

pip install django==2.2.5

pip install matplotlib

pip install matplotlib

First, create a project go to directory where you want to make your project. Then make project using this line — “django-admin startproject projectname”

Then go to the created project folder and check whether your Django server is running properly or not command be like “python manage.py runserver”

python manage.py runserver

Then go to your browser and put http://127.0.0.1:8000/ in the search section it seems like this —

Now make an app in the project folder — “python manage.py startapp yourappname” in my case my app name is matplot and project name is matdjango.

Now your directory looks like -

Project directory

Now go to setting.py and register your newly created app —

settings.py

Now go to urls.py in the root project file and redirect all the URLs to the app url —

project level urls.py

Now create a file name urls.py in the newly created app which is matplot in my case and make urlpattern for home page then make templates folder in the app level then make home.html file in it —

app level urls.py

Now go to views.py and make a function which render home.html and show graph of matplotlib-

app level views.py

Don’t forget to edit in home.html file —

home.html

Finally, run your Django server go to browser and output should be like —

Result

Finally, hope you will get the same result using the above instructions if you have any doubt put it into the comment section.

If you face any issue regarding code I will put in on my git link which is given below-

https://github.com/mdhvkothari/Python-Program/tree/master/matplot%20in%20django/matdjango

--

--

Madhav Kothari
Madhav Kothari

Responses (8)