SachTech Solution Pvt. Ltd.
Creating Web Application
using Django
● Templates are a key part to
understanding how Django really works
and interacts with your website
(HTML,CSS).
● Later on we will discuss, how templates
interact with database models and create
dynamic data.
● For now we will discuss about basics of
templates.
● Template contains static part of our
website as well as dynamic with the help
of template tags
● Now the question is what are template
tags.
● Templates adds special language to
HTML pages called template language.
● Django Template Tags are simple Python
functions which accepts one or more
value, an optional argument, process
those values and return a value to be
displayed on the page.
● Basically template tags allows us to add
dynamic content to HTML page.
● Create directory inside project folder
(template)
● Change proj> proj> settings.py file
○ TEMPLATE_DIR= os.path.join(BASE_DIR,”template”)
○ Add TEMPLATE_DIR to TEMPLATE list >dictionary “DIRS”
● Create view > use render function to
return HTML page as a response
● Map URLS to that view
DJANGO STATICFILES
Add media to your website
Images / Bootstrap / Javascript
etc.
Image Source
● So far, we have done Django templates
and how to insert dynamic data into
templates
● Now we will move further to Django
staticfiles.
● As in our previous lecture, we have
created a very simple HTML page.
● What if we want to add some styles,
animation to our website?
● The answer is Django staticfiles.
● Websites generally need to serve
additional files such as images,
JavaScript, or CSS. In Django, we refer to
these files as “static files”.
Steps to add staticfiles
● First of all, we will create a new
directory/folder inside of the project
called static ( just like we did for
templates)
● Then we will add this folder path to the
project’s settings.py file
●
Steps to add staticfiles
● Once we’ve done that we
need a place to store our
static image/bootstrap files
● You can create different
directories inside static
directory for image, CSS, JS
Steps to add staticfiles
● Now set up template tags
inside HTML file
○ {% load static %}
● Then to insert the image with
an HTML <img> tag using:
○ <img src={%static
“images/img.jpg” %} />
Relative URLs
with Templates
Relative URL with templates
● So far we write a hard code
url to open a page
● Now we will discuss about
how to use anchor tag to
access django views
● Here we will use name
Relative URL with templates
● Also we had discussed we
can create multiple apps in
single project
● We also create different urls
for different apps to avoid
complexity
TEMPLATE
INHERITANCE
TEMPLATE INHERITANCE
● Django follows DRY coding
principles.
● Template inheritance allows
us to create a base template,
we can inherit from.
● A base template can have
TEMPLATE INHERITANCE
● Template inheritance saves a
lot of time and maintain
same look for all pages.
● To create a child template
from base template we use
“extends” keyword inside
Django
Administration
DJANGO ADMIN
● The main part of a website is
to accept information from a
user, store it on to database
and maintain this
information.
● Almost all website has an
Who is admin?
● An administrator (admin) is
someone who can make
changes on a website that
will affect other users.
● Admin can change security
settings, add user, delete user
DJANGO ADMIN
● Luckily, Django has an
built-in admin panel.
● So,it saves a lot of time and
effort to design and program
admin part manually.
DJANGO ADMIN commands
● python manage.py makemigrations
appname
● python manage.py migrate
TO CREATE USER
● python manage.py createsuperuser
○ username,email,password
Django
Models
DJANGO MODELS
● We use Django models to
include database in our
Django Project.
● By default Django uses
SQLite, but we can customize
it according to requirement.
DJANGO MODELS
● In settings.py you can change
ENGINE parameter to custom
database.
● To create database tables we
will use app’s models.py file.
● We inherit table class from
Customizing
Admin Panel
CUSTOMIZING ADMIN PANEL
● We can customize admin
interface by doing:
○ Change order of fields
○ Adding search field
○ Adding filters to a table
○ Create a table view
CUSTOMIZING ADMIN PANEL
● Automatic Date Time Field
● Change UTC time to
Asia/Kolkata
● Add class Meta to remove
extra “s”
Save HTML form
data to
to Database
What is HTTP?
● HTTP stands for Hypertext
Transfer Protocol and is
designed to enable
communication between a
client and a server.
● The client submits a request,
GET & POST
● The most commonly used
methods for this
request/response protocol are
GET and POST.
● GET - requests data from a
resource
What is CSRF?
● This is a Cross-Site Request
Forgery (CSRF) token,
which secures the HTTP
POST action that is
initiated on the
subsequent submission of
What is CSRF?
● If it is not there, your form
may not work!
● It works by using a “hidden
input” which is a random
code and checking that it
matches the user’s local
Working with files
(Django)
Working with files
● So far, we had discussed a lot
about Django Model
Template Views etc.
● Now we will move further to
upload files on using Django.
● We can use Django’s
Working with files
● First of all we will create a
media folder inside project
folder.
● Next step is to add media
path to settings.py file.
● Django stores all uploaded
Working with files
● We can upload all types of
files (.jpg, .mpeg, .mp4,
.ogg,.pdf etc.) with the help of
file filed.
● We can extract data and
show these files in HTML
Working with files
● Create media folder
● Change settings and urls file
● Create a model > migrations
> migrate
● Register model in admin.py
● Extract model fields to HTML
Django Registration
& User Model
Django registration
● It is time to move further to
Registration and Login part.
● For this, we need to
understand what is User
Modal.
● User model is django’s built
USER MODEL
● The User object has a few key
features:
○ Username
○ Email
○ Password
○ First Name
USER MODEL
● There are also some other
attributes for the User
object, such as is_active,
is_staff, is_superuser etc.
● Sometimes you will also
want to add more
USER MODEL
● We can do this in your
applications models.py file
by creating another class
that has a relationship to
the User class.
● Let’s get started!!!
AJAX with Python
& Django
What is ajax
● AJAX stands for
Asynchronous JavaScript
and XML (eXtensible
Markup Language)
● With AJAX we can send
and retrieve data on server
AJAX APPLICATION
● Registration (Website)
● Online Cricket Scoring
● Cart
Django Login,
Logout, auth
DJNAGO LOGIN SYSTEM
● In this section we will
discuss about login system
in django.
● For this we will use
django’s built in
authentication system.
DJNAGO LOGIN SYSTEM
● HttpResponseRedirect()
according to user type
● Template inheritance in
dashboard page
○ Base.html >
dashboard_base.html
DJNAGO LOGIN SYSTEM
● Fetch Login User data in
HTML
● logout user
● Customize options using
built-in authentication
● Login required decorator
Selected Select Option
in Templates
Foreign
Key
What is Foreign key?
● A foreign key is a column
or group of columns in a
relational database table
that provides a link
between data in two tables
● A foreign key can have
What is Foreign key?
● A primary key used by
foreign key is also known
as parent key.
● The table where the parent
key from is known as
parent table.
What is Foreign key?
Model Forms
In
Django
Model Forms in Django
● Create model (database
table)
● Create forms.py file inside
app folder
● Create form class in
forms.py file
What is Foreign key?
● Import form class to
view.py file
● Render form object to
templates using context
dictionary
● Fetch form to html file
STEPS TO ADD BOOTSTRAP IN
DJANGO FORMS
● Install bootstrap4 library
○ Pip install
django-bootstrap4
● Add bootstrap4 to project’s
settings.py
● Load bootstrap4 to html
Cookies
In
Django
● A cookies is a
What are small amount
cookies? of a data from a
specific
website stored
in user’s
computer