Matplotlib into Django Template
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 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”
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 -
Now go to setting.py and register your newly created app —
Now go to urls.py in the root project file and redirect all the URLs to the app url —
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 —
Now go to views.py and make a function which render home.html and show graph of matplotlib-
Don’t forget to edit in home.html file —
Finally, run your Django server go to browser and output should be like —
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