Hello World, Welcome to projectsplaza.com. Today I am going to discuss how to create a bar chart image with the Matplotlib in Django 3. We will create a bar chart according to data and save a png image and then display this image in the Django template. I have divided this tutorial into the following parts:
Hello World, Welcome to ProjectsPlaza.com. Today I am going to discuss how to share common data in all Django templates. In this tutorial, we will show common data in all templates with the help of a context processor.
context_processors is a list of dotted Python paths to callables that are used to populate the context when a template is rendered with a request. These callables take a request object as their argument and return a dictionary of items to be merged into the context.
We will create a very simple example. In this example, we will create some view methods and their relative template. Then we will pass dictionary data to context processor and show in all templates.
I have assumed that you have basic knowledge of Django. If you are new to Django then please see our beginner tutorials.
I have divided this tutorial into the following steps:
Create shareData app and add to project
Create two view method index and show with their relative HTML templates
Create a URL and add to the project to show the template view
Create a third view method to pass data in a context processor
Hello World, Welcome to projectsplaza.com. Today I am going to discuss how to create a piechart with the Matplotlib in Django 3. We will create a piechart according to data and save a png image and then display this image in the Django template. I have divided this tutorial into the following parts:
Matplotlib introduction
Install Matplotlib
Create a piechart view method
Show piechart in the template view
Matplotlib Introduction
According to the official website: Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
Install Matplotlib
First of all, we will install the Matplotlib package with the following command.
pip install matplotlib
Add this package to your project-level settings.py in the installed apps list.
Create a view method for piechart
In this step, we will create a method to create a piechart with the matplotlib package. Add the following method in views.py file.
# MatPlotLib
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
import numpy as np
# Pie Chart
def piechart(request):
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Sale', 'Purchase'
sizes = [random.randint(10,30), random.randint(30,50)]
explode = (0.1, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.savefig('media/sale_purchase_peichart.png',dpi=100)
return render(request,'piechart.html')
In the above code, We are passing random numbers for sale and purchase value. Every time you refresh you will see the different piechart. You can replace the random number with dynamic values. We are showing a piechart as an image so we will do the following things to show the image.
Add the following code to the urls.py file.
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns=[
path('',views.piechart,name='piechart')
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Create a media directory as root because we are saving the image in this folder.
Show piechart in the template view
Till now we have saved the piechart in the folder. In this step, we will show the image in the Django template.
Create a piechart.html file in the templates folder and add the following code.
Hello World, Welcome to ProjectsPlaza.com. Today we will create login signup and logout with the user model in Django 3. In this tutorial, we will use the user model which comes with Django. Signup functionality does not come with Django but provides a UserCreationForm class to create signup functionality. This is a small but full-fledged app. You can download this app from GitHub. I will create an accounts app in an existing project. I have divided this tutorial into the following parts:
Create accounts app
Add accounts app in the project
Url setting for accounts app
Create a login view
Create Signup view
Login and Logout redirect setting
I have assumed that you have installed django and you have already a project. If not then please see my todo tutorial.
Create accounts app
In this step, we will create accounts app with the following command
py manage.py startapp accounts
Add accounts app in the project
In this step, we will connect our newly created app with a project in projects/settings.py
INSTALLED_APPS = [
...
'accounts',
]
Url settings for accounts app
In this step, we will connect our accounts app URL with the project URL. Open project/urls.py and add the following code.
Here we have attached two paths. One is from Django auth functionality and another is from the accounts app. We need to create accounts/urls.py and add the following code.
from django.urls import path
from .views import SignUp,home
urlpatterns=[
path('home/',home,name='home'),
path('signup/',SignUp.as_view(),name='signup'),
]
In the next section, we will create a login, signup, and home views.
Create a login view
When we try to access http://127.0.0.1:8000/accounts/login, it will try to find associated login file in accounts/templates/registration/login.html. So let’s create the login.html file in the relative folder and add the following code.
When you run the server, you will see the following screenshot.
Create Signup view
Open accounts.views.py file and add the following code in it.
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.forms import UserCreationForm
def home(request):
return render(request,'home.html')
# Sign Up
class SignUp(generic.CreateView):
form_class=UserCreationForm
success_url=reverse_lazy('login')
template_name='registration/signup.html'
As you can see, we have created two methods. One if for the home template and another is for the signup template. When the user successfully login, logout, or signup then we will redirect to this home view.
When you access the http://127.0.0.1:8000/accounts/signup, You will see the following screen
Login and Logout redirect Setting
In this step, we will set up redirect after a successful login or after successful logout. Open project settings.py file and add the following next-to-last-line.
Hello World, Welcome to ProjectsPlaza.com. Today I am going to discuss how to add and update data with ModelForm in Django 3. In this tutorial, We will create a simple todo application with add, update, and delete functionality. I have divided this tutorial into the following steps:
Create a learning project
Create todo app
Create todo Model
Create add and update form from todo model
Delete todo item
Add bootstrap 4 in the templates
This is a small but full-fledged application built with Django 3 and bootstrap 4. You can download source code from GitHub.
Hello World, Welcome to ProjectPlaza.com. Today we will discuss how to upload image in Django. We will create a simple form to upload a single image. We will upload an image with Django core FileSystemStorage package. I have divided this tutorial into the following parts:
I have assumed that you have basic knowledge of Django. If not then please see this tutorial.
Hello World, Welcome to ProjectsPlaza.com. Today we will discuss how to create a search field in the Django admin panel. I am using Django 3. Django is a very powerful MVT (Model View Template) web Framework built with python. It comes with lots of features.
For creating a search option in the Django admin panel, We just need to activate the search features. If you have not created an admin panel in Django then please have a look at the following tutorial 👇.
Hello World, Welcome to ProjectsPlaza.com. Today we will discuss how to use annotate aggregation in Django. In this tutorial, we will fetch data from two different models. For example, I am creating a video sharing website like YouTube. I have two models. One is a channel and another is a video. Both models are joined with foreign keys. Now I want to fetch channels with videos count. I will do this with annotate aggregation.
Hello World, Welcome to projects plaza.com. Today we will learn how to create a class-based view in Django 3. In this tutorial, we will render a simple template via class. Basically, there are two methods to load the view template in Django. One is a function-based view(FBV) and another is a class-based view(CBV). Class-based views were introduced in Django 1.7. The reason behind this was the proper structure, inheritance and the developer can customize the class-based view more than the function-based view.
Create cbv.html in the templates folder in your app and add the following code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CBV View</title>
</head>
<body>
<h3>Hello World, This is class based view</h3>
</body>
</html>
Hello World, Welcome to projectsplaza.com. Today we will learn how to save form data in the database in django. We will use the SQL database. I am assuming that you have installed Django in your system, If not then please refer to this tutorial. We will create a simple contact form in which the user will fill his basic information like full name, email, and message.
I have divided this tutorial into the following parts.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptRead More
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.