Python Django Complete PDF
Python Django Complete PDF
Python Django Complete PDF
com/
CLASS-XII
COMPUTER SCIENCE-PYTHON (083)
After installation of Python and Django in computer, next step is install the
IDE (Integrated Development Environment) to write the code for web
application. Here, we shall use PyCharm as IDE.
PyCharm : PyCharm is an IDE used in computer programming, specifically for
the Python language.
To download PyCharm, go to the following link:
https://www.jetbrains.com/pycharm/download/
Install the setup.
Now your system is ready to develop a web application in PyCharm using
Django.
https://pythonschoolkvs.wordpress.com/ Page 1
3.4 Create a Project and web application:
Create the application under SPORTSMEET Project. Here the application name
is NATIONALS. To create this application, type the following command in
terminal window of PyCharm.
D:\SPORTSMEET>python manage.py startapp NATIONALS
Now to start the Django server, Type following command in Terminal window
of PyCharm.
D:\SPORTSMEET>python manage.py runserver
https://pythonschoolkvs.wordpress.com/ Page 2
So, we have created a project SPORTSMEET and a web application NATIONALS under
this project.
In this web application we will perform the CRUD (Create, Read, Update,
Delete) operations.
We shall store the data of those students who will participate in National
Sports Meet and perform the CRUD operations with the data stored in
database.
To Store the data, we will use sqlite3 database system which is in-built in
PyCharm. So no need to install it separately. You can use MySQL database
system, which we will discuss later.
Open PyCharm.
Open settings.py file under SPORTSMEET subfolder to add our application in
installed apps for migration.
Example:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'NATIONALS'
]
https://pythonschoolkvs.wordpress.com/ Page 3
Open the file models.py under NATIONALS web application to create a model
and its fields. Models are considered as tables in MVC framework. An
application may have multiple models.
from django.db import models
class PLAYER(models.Model):
pid = models.CharField(max_length=10)
pname = models.CharField(max_length=50)
dob = models.CharField(max_length=20)
gender = models.CharField(max_length=10)
game = models.CharField(max_length=30)
region = models.CharField(max_length=35)
class Meta:
db_table = 'player'
Here, class Meta is as an instance of class PLAYER, that executes with Meta
class.
Now create a file forms.py under NATIONALS by right click on NATIONALS.
Django provides the facility of forms. It means we need not create any HTML
form for table fields. Django denotes the table fields as a form. For that we
have to create a forms.py file and write the following code in forms.py file.
https://pythonschoolkvs.wordpress.com/ Page 4
from django import forms
from NATIONALS.models import PLAYER
class PLAYERFORM (forms.ModelForm):
class Meta:
model = PLAYER
fields = "__all__"
Now migrate the models in database. For this, we have to run the following
commands in Terminal window of PyCharm:
D:\SPORTSMEET>python manage.py makemigrations
This command will create the model and make the changes if any. If the
command successfully runs, the following information will be displayed in
Terminal window.
Migrations for 'NATIONALS':
NATIONALS\migrations\0001_initial.py
- Create model PLAYER
Now run another command to migrate the created model. In Terminal
window, write the following command:
D:\SPORTSMEET>python manage.py migrate
Now the created model is stored in the file 0001_initial.py under the folder
migration in NATIONALS application.
https://pythonschoolkvs.wordpress.com/ Page 5
templates folder will have the .html files. In our application, we will have
index.html, edit.html and show.html files under templates folder.
Create the html files with the name index.html, edit.html and show.html, by
right click on templates folder under NATIONALS.
Create another folder with the name static, under NATIONALS. It will have
style.css file.
Right click on static folderNewFilewrite the name style.cssOK
Write following code in style.css file:
table, th, td {
border: 1px solid black;
}
div {
font-weight:bold;
}
https://pythonschoolkvs.wordpress.com/ Page 6
# Create your views here.
def ply(request):
if request.method == "POST":
form = PLAYERFORM(request.POST)
if form.is_valid():
try:
form.save()
return redirect('/show')
except:
pass
else:
form = PLAYERFORM()
return render(request,"index.html",{'form':form})
def show(request):
players = PLAYER.objects.all()
return render(request,"show.html",{'players':players})
Open index.html file under templates folder and write the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index Page</title>
{% load staticfiles %}
<link rel="stylesheet" href="{% static '/style.css' %}" />
</head>
<body>
<center><strong><u><h1>PLAYER DATA</h1></u></strong></center>
<center>
https://pythonschoolkvs.wordpress.com/ Page 7
<br>
<div class = "form-group row">
<label class="col-sm-1 col-form-label"></label>
<div class="col-sm-4">
<h3>Enter Details</h3>
</div>
</div>
<div class = "form-group row" >
<label class="col-sm-2 col-form-label">Player ID:
</label>
{{ form.pid }}
</div>
<br>
https://pythonschoolkvs.wordpress.com/ Page 8
<button type="submit" class="btn btn-
primary">Submit</button>
</div>
</div>
</div>
</form></center>
</body>
</html>
Open show.html file under templates folder and write the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Show Player Details</title>
{% load staticfiles %}
<link rel = "stylesheet " href="{% static '/style.css' %}"
/>
</head>
<body>
<center><strong><u><h1>PLAYER
DETAILS</h1></u></strong></center>
<center>
<table class="table table-striped table-bordered table=80%">
<thead class= "thead-dark">
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>Date of Birth</th>
<th>Gender</th>
<th>Game</th>
<th>Region</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
{% for player in players %}
<tr>
<td>{{ player.pid }}</td>
<td>{{ player.pname }}</td>
<td>{{ player.dob }}</td>
<td>{{ player.gender }}</td>
<td>{{ player.game }}</td>
<td>{{ player.region }}</td>
<td>
<a href = "/edit/{{player.id}}"><span
class="glyphicon glyphicon-pencil">Edit</span> </a>
</td>
<td>
<a href="/delete/{{player.id}}">Delete</a>
</td>
https://pythonschoolkvs.wordpress.com/ Page 9
</tr>
{% endfor %}
</tbody>
</table>
</center>
<br>
<br>
<center><a href="/ply" class="btn btn-primary">Add New
Record</a></center>
</body>
</html>
Open edit.html file under templates folder and write the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Edit Record </title>
{% load staticfiles %}
<link rel = "stylesheet" href = "{% static '/style.css' %}"
/>
</head>
<body>
<center>
<form method="POST" class="post-form"
action="/update/{{player.id}}" >
{% csrf_token %}
<div class ="container">
<br>
<div class="form-group row">
<label class="col-sm-1 col-form-label"></label>
<div class = "col-sm-4">
<h3>Update Details</h3>
</div>
</div>
<br>
https://pythonschoolkvs.wordpress.com/ Page 10
<br>
</div>
</form>
</center>
</body>
</html>
Open urls.py under SPORTSMEET subfolder. And refer the methods. Write
the following code in urls.py file.
https://pythonschoolkvs.wordpress.com/ Page 11
from django.contrib import admin
from django.urls import path
from NATIONALS import views
urlpatterns = [
path('admin/', admin.site.urls),
path('ply', views.ply),
path('show', views.show),
path('edit/<int:id>', views.edit),
path('update/<int:id>', views.update),
path('delete/<int:id>', views.delete),
]
After that save the project. Now open Terminal window in PyCharm, Write
the following command:
D:\SPORTSMEET>Python manage.py runserver
After execution of above command, the following address will appear in
Terminal window.
http://127.0.0.1:8000/
Copy it and paste it into web browser. Press Enter. It will display the error
page. Now write the address of the show page. Means, write the address
given below:
http://127.0.0.1:8000/show
OUTPUTS:
https://pythonschoolkvs.wordpress.com/ Page 12
OUTPUT-1 Empty Table, No Records
https://pythonschoolkvs.wordpress.com/ Page 13
OUTPUT-3 Records in the table after addition
https://pythonschoolkvs.wordpress.com/ Page 14