0% found this document useful (0 votes)
88 views151 pages

Chapter 15 Creating A DJANGO Based Basic Web Application

This document provides an introduction to creating a basic web application using the Django framework in Python. It begins with definitions of key terms like web application, web framework, and library. It then discusses popular Python web frameworks like Django, CherryPy, Pylons, and Flask. The document introduces Django as a free and open-source Python web framework designed for rapid development. It notes that Django was created in 2003 and discusses its goals of being fast, simple, secure, and suitable for any web application project.

Uploaded by

besiren38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views151 pages

Chapter 15 Creating A DJANGO Based Basic Web Application

This document provides an introduction to creating a basic web application using the Django framework in Python. It begins with definitions of key terms like web application, web framework, and library. It then discusses popular Python web frameworks like Django, CherryPy, Pylons, and Flask. The document introduces Django as a free and open-source Python web framework designed for rapid development. It notes that Django was created in 2003 and discusses its goals of being fast, simple, secure, and suitable for any web application project.

Uploaded by

besiren38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 151

CHAPTER - XV

CREATING A DJANGO BASED BASIC


WEB APPLICATION
Class XII

Unit I
DATA MANAGEMENT ( DM – II)
(20 Theory + 20 Practical)

Prepared by
Praveen M Jigajinni
DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci)

Department of Computer Science, Sainik School Amaravathinagar

Cell No: 9431453730 Courtesy CBSE


Reference Book
Sumita Arora
INTRODUCTION
Web INTRODUCTION
App

What is Web Based Application?


In computing, a web application
or web app is a client–server computer
program which the client runs in a web
browser. Common web applications include
webmail, online retail sales, and online
auction
Web INTRODUCTION
App

Examples of Web based Applications:-

Web applications include online forms,


shopping carts, word processors, spreadsheets,
video and photo editing, file conversion, file
scanning, and email programs such as Gmail,
Yahoo and AOL. Popular applications include
Google Apps and Microsoft 365.
WEB FRAME WORK
WEB FRAME WORK

What is Web Frame Work?

A WEB FRAME WORK is a software tool


that provides way to build and run dynamic
websites and web enabled applications.
WEB ENABLED APPLICATIONS
WEB ENABLED APPLICATIONS

What is Web Enabled Application?

Web enabled refers to a product or service


that can be used through, or in conjunction
with, the World Wide Web. A Web-
enabled product may be accessed through
a Web browser or be able to connect to
other Web-based applications in order to
synchronize data.
LIBRARY
LIBRARY

What is Library?

Library is a collection of functions and


methods that allows you to perform many
actions without writing your code. For
example, the Python imaging library (PIL) is
one of the core libraries for image
manipulation in Python.
Difference between Framework and Library
Difference between Framework and Library

Framework

1. The Framework defines the architecture of


your code.

Library
1. The Library does not defines the
architecture of your code. It makes no
assumption on how your app is structured, it just
gives you a patch function to apply.
Difference between Framework and Library

Framework

2. Framework acts like a foundation of your


house.

Library

2. The library is the garage that you have your


tools in.
Difference between Framework and Library

Framework

3. Framework inversion of control,


framework calls you.

Library

3. You are in control when you call a method


from the library.
Difference between Framework and Library

Framework

Web Framework is some what like as if you


have purchased fully designed flat where in you
lifted some windows and doors as per your liking.

Library
The library on the other hand is somewhat
like as if you are building your own house but you
are using some predesigned doors and windows.
Popular Python Frameworks
Popular Python Frameworks
Popular Python Frameworks

CHERRYPY
Popular Python Frameworks
Popular Python Frameworks

WEBWARE
Popular Python Frameworks

PYLONS
Popular Python Frameworks
Popular Python Frameworks
Popular Python Frameworks

Dash
Popular Python Frameworks
Popular Python Frameworks
Popular Python Frameworks
Popular Python Frameworks
Popular Python Frameworks

Hug
Popular Python Frameworks
Popular Python Frameworks
Popular Python Frameworks

SANIC
Popular Python Frameworks

TORNADO
Popular Python Frameworks

Turbogears
Popular Python Frameworks

AIOhttp
Popular Python Frameworks

SELENIUM
Popular Python Frameworks

Googleapp Engine
Popular Python Frameworks

ROBOT
Popular Python Frameworks
Popular Python Frameworks
How Web, Website and Web Application Work?
How Web, Website and Web Application Work?

The web work in the form of Client Server


Architecture. Your Web Browser acts like client
(web client / Front End) and the Web Server with
which it interacts is the Server (Back End).

The Client Browser mostly work on HTTP


protocol.

The Client make an HTTP request, which web


server responds.
How Web, Website and Web Application Work?

The Client makes these two types of


requests:

1. HTTP GET request

2. HTTP POST request


How Web, Website and Web Application Work?

1. HTTP GET request

An HTTP GET request refers to the way of


reserving information from the web server using
given URL on the web browser.
If no such URL exists, web server returns an
error code (often error no 404 that you see on
your web browser).
How Web, Website and Web Application Work?

2. HTTP POST request

An HTTP POST request is a way to send data


to the server for example data filled in an online
form (Online UPSC Form filling is the best
example), file upload using HTML forms over
webpage.
How Web, Website and Web Application Work?
INTRODUCING DJANGO
INTRODUCING DJANGO

What is DJANGO?

Django is a Python-based free and open-


source web framework, which follows the
model-template-view architectural pattern. It is
maintained by the Django Software
Foundation, an independent organization
established as a 501 non-profit. Django's
primary goal is to ease the creation of complex,
database-driven websites
Source : Wikipedia
INTRODUCING DJANGO

What is DJANGO?

Django is an open-source python web


framework used for rapid development,
pragmatic(sensible/realistic), maintainable,
clean design, and secures websites. It takes
care of a lot of hassle involved in the web
development; enables users to focus on
developing components needed for their
application.
Who developed Django Framework?
Who developed Django Framework?

Django was created in the fall of 2003, when the


web programmers at the Lawrence Journal-World
newspaper, Adrian Holovaty and Simon Willison, began
using Python to build applications. It was released
publicly under a BSD (Berkeley Software Distribution)
license in July 2005. The framework was named after
guitarist Django Reinhardt.
Contd..
Who developed Django Framework?
Adrian Holovaty (born 1981) is an American web
developer, journalist and entrepreneur from Chicago, Illinois,
living in Amsterdam, the Netherlands. He is co-creator of the
Django web framework and an advocate of "journalism via
computer programming". Born 1981 (age 37–38)
Naperville, Illinois
Nationality USA
Alma mater Missouri School of
Journalism (B.A., 2001)
Occupation web developer,
journalist,
entrepreneur
Known for Django Web
framework

Source : Wikipedia Contd..


Who developed Django Framework?
Simon Willison Born - January 1981 (age 38) is a
British programmer, co-founder of the social
conference directory Lanyrd, and Director of
Architecture at Eventbrite.

Originating from the UK, he


currently resides in San Francisco,
California.

Simon is a co-creator of
the Django Web framework and is
a frequent public speaker.
What does the name Django mean?
What does the name Django mean?

Django is a Romany term meaning


"I awake".
It is best known as the nickname of
Belgian jazz guitarist Jean Baptiste "Django
Reinhardt” (23 January 1910 – 16 May 1953),
whose fame has led to its use throughout
music. The framework was named after
guitarist Django Reinhardt. In June 2008.
What does the name Django mean?

Django Reinhardt
(23 January 1910 – 16 May 1953)
Why DJANGO?
Why DJANGO?

01. It’s fast and simple

02. It’s secure

03. It suits any web application project

04. It’s well-established


Why Django?

01. It’s fast and simple


Why Django?

01. It’s fast and simple

One of Django’s main goals is to simplify


work for developers. To do that, the Django
framework uses:
The principles of rapid development, which
means developers can do more than one
iteration at a time without starting the whole
schedule from scratch;
Why Django?

01. It’s fast and simple

DRY philosophy - Don’t Repeat Yourself -


which means developers can reuse existing
code and focus on the unique one.
Why Django?

02. It’s secure


Why Django?

02. It’s secure

Security is also a high priority for Django.


It has one of the best out-of-the-box security
systems out there, and it helps developers
avoid common security issues, including:
 CLICKJACKING
 CROSS-SITE SCRIPTING
SQL INJECTION (SQLi)
Why Django?

02. It’s secure


Django promptly releases new security
patches. It’s usually the first one to respond to
vulnerabilities and alert other frameworks.
 CLICKJACKING
The malicious practice of manipulating a
website user's activity by concealing hyperlinks
beneath legitimate clickable content, thereby
causing the user to perform actions of which
they are unaware.
Why Django?

02. It’s secure


 CROSS-SITE SCRIPTING
Cross-site scripting (XSS) is a type
of computer security vulnerability typically
found in web applications. XSS enables attackers
to inject client-side scripts into web
pages viewed by other users. A cross-site
scripting vulnerability may be used by attackers
to bypass access controls such as the same-
origin policy.
Why Django?

02. It’s secure


SQL INJECTION (SQLi)
SQL injection is a code injection
technique, used to attack data-driven
applications, in which malicious SQL statements
are inserted into an entry field for execution.
Example of a Union-Based SQL Injection
One of the most common types of SQL
Injection uses the UNION operator.
Contd..
Why Django?

02. It’s secure


SQL INJECTION (SQLi)
Example of a Union-Based SQL Injection
One of the most common types of SQL
Injection uses the UNION operator. It allows the
attacker to combine the results of two or more
SELECT statements into a single result. The
technique is called union-based SQL Injection.
The following is an example of this technique.
Why Django?

03. It suits any web application project

Versatile
Why Django?

03. It suits any web application project

With Django, you can tackle projects of


any size and capacity, whether it’s a simple
website or a high-load web application. Why
use Django for your project? Because:

It’s fully loaded with extras and scalable, so


you can make applications that handle heavy
traffic and large volumes of information;
Why Django?

03. It suits any web application project

It is cross-platform, meaning that your project


can be based on Mac, Linux or PC;

It works with most major databases and allows


using a database that is more suitable in a
particular project, or even multiple databases at
the same time.
Why Django?

04. It’s well-established


Why Django?

04. It’s well-established


Django is time- and crowd-tested. It has a big,
supportive community accessed through numerous
forums, channels, and dedicated websites. It’s easy to
find help when there’s a problematic function in the
code, and to find developers if your company is looking
to base the next project on Django.

Django started off with great documentation, the best


of any other open-source framework.
What you can do with Django?
What you can do with Django?

Here’s an interesting fact: Django was first created


to power a web application for a newspaper
publisher, the Lawrence Journal-World. You can
expect it to be amazing at handling projects with
volumes of text content, media files, and heavy
traffic — or anything else that works like a web-
based periodical.

Django is also used to build eCommerce websites


and health care and financial applications for
transportation and booking, social media sites, and
more
Famous Companies that use the Django Framework
Famous Companies that use the Django Framework

Here’s an interesting fact: Django was first created to


power a web application for a newspaper publisher,
the Lawrence Journal-World. You can expect it to be
amazing at handling projects with volumes of text
content, media files, and heavy traffic — or anything
else that works like a web-based periodical.

Django is also used to build eCommerce websites


and health care and financial applications for
transportation and booking, social media sites, and
more
Famous Companies that use the Django Framework

NEWS PAPER
Installing Django
Installing Django

1. Download python from ​www.python.org​.


Select Python 3 Release, it will take you to
the downloads page. Select the Windows
x86-64 web based installer.

2. Once the file is downloaded start the


setup. Make sure to check the "Add Python
3.6.4 to PATH" or "Add Python 3.7 to PATH“
depending on your OS
Installing Django

Click on the "Customize Installation" part and check


whether all the options are ticked.
Installing Django
Installing Django (online)

3. Go to start and search for "Windows


Powershell". Right-click on it and select the
"Run as administrator" option.
4. 1] After opening the Windows powershell,
we need to install virtualenvwrapper. It is used
for creating isolated environments.

pip install virtualenvwrapper-win


Installing Django(online)

2] Now, we'll create a virtual environment for a


project

mkvirtualenv mywebsite
3] To activate the environment mywebsite we
have to run the following command.

workon mywebsite
Installing Django(online)

4] Next, we are going to install Django with the


pip command
pip install django

5] You can check the django version by


executing the following command.
python -m django --version
6] We'll set the current directory to C drive
Cd\
Installing Django(online)

7] The next step is to create a new directory to


store the new project that we are going to work
on. In this case it will be webdesign.
mkdir newdir
Once you create the directory, set it as the
current directory
cd newdir
Installing Django(online)

8] Now, we need to startproject with the

django-admin startproject projectname

where we will replace the projectname with a


name of the project.
For Example:
django-admin startproject webdesign
Installing Django(online)

9] Set the current directory to the project


directory i.e webdesign
cd webdesign

10] After that is done simply run the

python manage.py runserver

11] Open web browser and type


localhost:8000 or \\127.0.0
Installing Django(online)

Created project screenshot


Uninstalling Django
Uninstalling Django

You can uninstall django with a simple


command. To uninstall django windows 10 just
execute the following command.

pip uninstall django


What is Virtual Environment?
What is Virtual Environment?
Virtualenv is a useful tool which creates
isolated python environments.
 Suppose if you are working with
Application1 and is using some Django and
Python libraries.

 New Application2 is using same libraries


updated version.
Now what to do? If you update same
libraries App 1 will not work, if you do not
update App2 will not work
What is Virtual Environment?
Solution for this problem is the Virtual
Environment.

What is Virtual Environment?

A virtual environment is a tool that helps to


keep dependencies required by different
projects separate by creating isolated
python virtual environments for them. This is
one of the most important tools that most of
the Python developers use.
Django Offline Installation Steps
Django Offline Installation Steps

if you need to perform an offline installation of


Django:

1. Install Virtual Environment using following


command:-
2. Say your directory is C:\Django, Then Type

C:\Django> python -m virtualenv venv


Django Offline Installation Steps
Django Offline Installation Steps

if you need to perform an offline installation of


Django:

1. Download a Django release on a machine with


internet connection from
https://github.com/django/django/releases
2. As of Django 1.11, pytz is a dependency.
Download the latest wheel from pypi
3. Transfer the downloaded files to the offline PC
4. Install pytz: pip install pytz-2017.3-py2.py3-
none-any.whl (update filename as appropriate)
5. Install Django: pip install <release-name>.zip
Django Offline Installation Steps

After copying the file, use the following command


First Goto Django directory say c:\Django is the folder:

1. Open the Powershell /admin command prompt


2. Cd Django
3. Type the command

pip install pytz-2019.2-py2.py3-none-any.whl

4. Then install Django


pip install django-2.2.5.zip
Django Offline Installation Steps

POST INSTALLATION SCREEN SHOT


Django Offline Installation Steps

To check the version of Django that you have on


a Windows PC, open up the command prompt on
your Windows PC.
Once, you have it opened, type in the following
line.

py -m django --version
Django Offline Installation Steps

py -m django --version command will


produce following output
Django Project
Django Project

Django uses two important terms in project


development.

1. Project

Project refers to an entire software/ application.

2. An App

An App refers to one sub module catering to


one part of project.
Django Project
For example: Online Shopping Store project contains
following apps

1. Customer App This App stores customer


registration details such as name, address,phone no.
etc…
2. Products This App stores detials such as
product name, category,size,weight etc…

3. Shopping Cart This App will manage the


details of selected products chosen for shopping.
Creating Project
Creating Project
1. Go to django directory c:\Djang> Create a new
folder using mkdir
C:\Django>mkdir SSAProjects
2. Change directory to
C:\Django>CD SSAProjects
3. Now Create Virtual Environment by typing
C:\Django\SSAProjects>
python –m virtualenv venv
4. Activate the environment by typing command
activate
Creating Project
Create a new folder using mkdir
Creating Project
Change directory to SSAPorjects and
Create Virtual Environment. Then activate
Creating Project
After creating Virtual environment
following
files are created
Creating Project

If activate command is not working execute the


command

Set-ExecutionPolicy RemoteSigned

You can bypass this policy by adding -


ExecutionPolicy ByPass when running
PowerShell, type the command

powershell -executionpolicy bypass -file venv\


scripts\activate.ps1
Creating Project

5. On the command prompt C:\Django\


SSAProjects>
Type,

Django-admin startproject SSA1

SSA is the project name.

SCREEN SHOT - NEXT SLIDE


Creating Project

Creating SSA1 project.


Creating Project

Final Step is open your web browser and


Type:

Localhost:8000
Or
127.0.0.1:8000

127.0.0.1 is the ip address of your local host


and 8000 is the defalut port on which it
connects to Django webserver.
Creating Project
Web browser looks like

At Command prompt press Ctrl +Break to quit


the project
SKELETON/ STRUCTURE OF DJANGO PROJECT

OR

DJANGO PROJECT LAYOUT


SKELETON/ STRUCTURE OF DJANGO PROJECT

When you create project with startproject


command django automatically creates folder on same
project name. The project folder has one more folder
having same foldername and manage.py file.

Your project will look like this:


SKELETON/ STRUCTURE OF DJANGO PROJECT
SSA1 contains 2 items one is the folder same
name as project name SSA1 and manage.py
SKELETON/ STRUCTURE OF DJANGO PROJECT
SSA1/SSA1 contains 4 files
SKELETON/ STRUCTURE OF DJANGO PROJECT
SKELETON/ STRUCTURE OF DJANGO PROJECT

SSA1 Folder

SSA1 outer folder is the name of the


project where you find all files and folders
related to your application

SSA1 Folder (Sub Folder)


SSA1 inner folder is the project
configuration folder contains 4 files.
SKELETON/ STRUCTURE OF DJANGO PROJECT

SSA1 Folder (Sub Folder) – 4 Files

1. __init__.py

2. settings.py

3. urls.py

4. wsgi.py
SKELETON/ STRUCTURE OF DJANGO PROJECT

SSA1 Folder (Sub Folder) – 4 Files

1. __init__.py

The __init__.py file is empty and it exists


in the project for the sole purpose of telling
the python interpreter that this directory is a
package. That’s one of the standard rules of
python packages
Although we won’t be doing anything on
this file
SKELETON/ STRUCTURE OF DJANGO PROJECT

SSA1 Folder (Sub Folder) – 4 Files


2. settings.py
It is the main file where we will be adding
all our applications and middleware
applications. As the name suggests this is the
main settings file of the Django project. This file
contains the installed applications and
middleware information which are installed on
this Django project.
SKELETON/ STRUCTURE OF DJANGO PROJECT

SSA1 Folder (Sub Folder) – 4 Files

3. urls.py

urls.py file contains the project level URL


information. URL is Universal Resource Locator
and it provides you with the address of the
resource (images, webpages, web-applications)
and other resources for your website.
The main purpose of this file is to connect the
web-apps with the project.
SKELETON/ STRUCTURE OF DJANGO PROJECT

SSA1 Folder (Sub Folder) – 4 Files

3. urls.py

Anything you will be typing in the URL bar


will be processed by this urls.py file. Then, it will
correspond your request to the designated app
you connected to it.
SKELETON/ STRUCTURE OF DJANGO PROJECT

SSA1 Folder (Sub Folder) – 4 Files


3. urls.py
Here this file by default adds one URL to the
admin app. The path () takes two arguments.
SKELETON/ STRUCTURE OF DJANGO PROJECT

SSA1 Folder (Sub Folder) – 4 Files

4. wsgi.py (Web Server Gateway Interface)


WSGI is a specification, laid out in PEP 333
(Python Enhancement Proposal 333), for a
standardized interface between Web servers
and Python Web frameworks/applications. The
goal is to provide a relatively simple yet
comprehensive interface capable of supporting
all (or most) interactions between a Web server
and a Web framework.
ROLE OF TWO PROJECT NAME FOLDERS
ROLE OF TWO PROJECT NAME FOLDERS

OUTER PROJECT

When you create a project with


django –admin startproject command, Django
creates two project folders having project name
and one is the sub folder of the other.
The outer project folder is the BASE
DIRECTORY / FOLDER of your project and holds
all the apps and other setting modules of the
project.
ROLE OF TWO PROJECT NAME FOLDERS

It also holds the administrative tasks


manage.py file in it.
The Folder also holds the templates folder
that creates presentation html files in it.
INNER PROJECT FOLDER

The inner project folder is a subfolder of


the base directory and is the project web-
application folder. It holds all the setting files of
your project.
Creating Apps of a Django Project

You need to create each app individually


inside the project by issuing the command:-

python manage.py startapp student


Creating Apps of a Django Project

python manage.py startapp student


Creating Apps of a Django Project
See the student app folder files
Creating Apps of a Django Project

Student app folder (Sub Folder) Files

Like project folder, App folder also


contains some files in it and they are:-

1. admin.py 5. views.py

2. apps.py 6. __init__.py

3. models.py 7. migrations/
4. tests.py
Creating Apps of a Django Project

Student app folder (Sub Folder) Files

Like project folder, App folder also


contains some files in it and they are:-

1. admin.py It is the configuration


file for the built in Django admin app.

2. apps.py It is the configuration


file for the app itself.
Creating Apps of a Django Project

Student app folder (Sub Folder) Files

3. models.py It is the file where you


define database models. Django will
automatically translate models defined
here in to database tables.

4. tests.py This file is for our app


specific test.
Creating Apps of a Django Project

Student app folder (Sub Folder) Files

5. views.py This file is for resolving


http requests/response for our web app.
6. __init__.py The __init__.py file is
empty and it exists in the project for the sole
purpose of telling the python interpreter that
this directory is a package. That’s one of the
standard rules of python packages.
Creating Apps of a Django Project

Student app folder (Sub Folder) Files

7. migrations/

This folder keeps track of changes in


models.py file and updates the database
accordingly.
Understanding Django Project Architecture
Understanding Django Project Architecture

Django projects are based on a software


architecture that separates:

1. Data being managed (Model)

2. Presenting or showing it to user(template)

3. Handling Logic (view)


This software architecture was called
MVC (Model View Controller) now it is
Understanding Django Project Architecture

called MTV (Model Template View)


architecture. The components of MTV
architecture are:-
1. Model

2. Template

3. View

4. Controller
Understanding Django Project Architecture

1. Model
This component is responsible for data
management. This deals with data access,
validation and relationships among the data.
The models defined in Django are mapped into
database tables.
2. Template
This component is responsible
presentation of data/ webpage to the user/
client.
Understanding Django Project Architecture

3. View
Django view acts like a bridge between
models and templates. A view access model data
and redirects it to a templates for presentation
4. Controller
It is the logic tier provided by the Django
framework and it links the components of Django
architecture i.e model,view and template. It
passes data for display from model to template
via view.
Understanding Django Project Architecture
Understanding Django Project Architecture
Register Apps
Register Apps

To register aaps with the project follow


the steps:-

Open inner folder’s settings.py file

you need to add the app names in the


INSTALLED_APPS
list without deleting any other settings.
Register Apps
SSA1/settings.py
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
‘students’
‘teachers’
]
THANK YOU

You might also like