Django The Ultimate Beginners Handbook - Mahesh Kumar
Django The Ultimate Beginners Handbook - Mahesh Kumar
THE ULTIMATE
Beginner’s Handbook
In this book all the important concepts of Django are explained,
which are more required in Web Development.
Author
Mahesh Kumar
Dedicate-
This book is for all those students who want to learn how to create Program for web
development. And if you want to make projects based on your ideas, then all of them
can take help of this book. With the help of this book, from 8 class students to 12 class
students. This book is also for all those students. Those who either doing engineering or
want to do engineering (Electronics Engineering, Electronics Communication
Engineering, Computer Science Engineering, Information Technology Engineering).
This book will help them to complete their basic for all of them. After reading this book
and using all its formulas, you will found the increment in your knowledge, And I can
say this with conviction.
hey guys today's session we'll be focusing on Django so Django is nothing
but a web development framework so without wasting any more time let's
move ahead and look at today's agenda
so these are the topics that we'll be covering in this session so we'll start
with the session by first discussing why do we need Django framework
once we get an answer to it will they understand what exactly is Django
after that we'll dig deeper into MVC MBT pattern that Django offers and
then we'll get started with it so here we'll start building a web application
from scratch and then moving on to some advanced concepts with it next
we'll be discussing some of the components or you can say the building
blocks of Django along with the packet all demonstration and finally
towards the end I'll be showing you a project which is a website where I
have incorporated all the components and some advanced stuff to make the
website look better so these are topics that will be covered in today's session
so let's get started with the very first topic that is why do we need Django
framework so Django is a Python web framework but why a framework
What is Django?
MVT architecture now MVC stands for Model View controller and MVC
stands for Model View template so don't worry guys we'll be discussing this
architecture in further most slides but first let's get a clearer picture of what
exactly Django is so we know that Django is built on a powerful language
status Python now it also contains some tools and open-source libraries
which makes a life of a web developer much easier as it helps him to build
a very easy flexible scalable and a maintainable web application so to
summarize in Django is used for creating web application that is the soft
sphere which is backed up by a database now it includes some kind of
interactivity that operates through a browser so Django framework just
provides your structure and some common methods for making this kind of
software so now let us move on to different features of Django
or what Django has to offer us so the very first feature of Django is it is fast
now how it is fast now Jambu is faster because it is very easy to code here
and it's very good whenever you have to meet some deadlines so that is the
very first reason why Django is preferred over any other frame book next
Django is fully loaded which means it has in numerous packages and
libraries to offer so instead of coding all the use those packages or use the
libraries which are already developed by django developers next django is
very secure so the security of any application is one of the major factor of
concern for any developer now with django you do not have to worry about
that and it's already taken care of and then django is scalable so django
divides the entire project in multiple components or you can say
applications now let's say you have to add another feature later on or you
just want to make changes in one of the existing features so what you can
do you can always go back to the code and change according to your wish
or you can also do one thing you can just go to your code just create a new
app and then link it by our URL so this is how a django is extremely
scalable next let us move ahead and look at the MVC MVT pattern that
django follows so I've already talked about it that MVC stands for Model
View controller and MVC stands
Django Architecture: MVC-MVT Pattern for Model View template so as
you can see here
you have three components that is Model View and a controller so model
basically represents your database or you can say it is responsible for
storing and maintenance of data next is your view so view is used for the
presentation part or you can say it displays the request portion of data to the
user and here is your controller so controller regulates the interaction
between the model and the view so this is all about Model View controller
then we have Model View template functionalities are exactly the same
model stands for the storing of data views just for the representation part
and here template is something new so people also already discuss that how
you can use template and how you can represent your data to the user so
this is how django MVC and MVP pattern are different now django itself's
take care of the controller part so it doesn't need a controller so that is a
major advantage of django now let us move ahead and look at the
architecture or how this nbcnb deepak trim actually works now as you can
see in this image
so here we have model so model represents your database or where your
data has been stored and then you have template so template can be a web
application or front-end as well now when I combine these two I have a
view and to connect to that view this is going to be a specific URL also we
can have a separate URL for different views now Django is an interface or
you can say the middleman as it just helps the user to connect to the exact
URL this is how Django works so I hope you got a basic understanding of
how Django works so what exactly happens is just a developer provides the
model the view and template just Maps it to a URL and Django just
performs the magic and serves it to the user so this is how Django works
now enough of practical knowledge so let's move on to the hands-on part
we will first create a basic application from Django Web Development
Example scratch so let's get started with it now to create a web application
you first need to install Django so for that first step is to go to this link and
then install the latest version of Django now before going ahead and
installing Django in a system we know that we are using Python to develop
website so the first thing that we need is Python now I think you get this
must be aware of the Python and its programming because that is what
we're using here so just to verify it that you have Python installed in your
system or not just go to the command prompt
so here you can see this download tab so just click on that
so once you click on that you will be redirected to this page which says that
you just have to type in pip install Django and with the latest version so
right now the latest official version is one point eleven point seven
also the next thing that you need to check is that do you have Pippen stalled
or not now what is pip pip is just a tool that allows you to download things
online so once you're done with it you can go to your command prompt and
then you can check whether Django exists your system or not so once we
have this command we can go back to a command prompt and see whether
we have Django installed or not so I will go again to my command prompt
so I have closed it by mistake so let me just open the game for you so this
command says tip install Django with the latest version that is one point
equal to equal to one point one one now as you can see here my ladies
William Django is installed and what they have done they have uninstalled
my previous version that was one point eleven point four so now it has
successfully uninstalled this version
and currently my Django one point eleven point seven is installed in my
system so let me go to any IDE and let me help you to create a basic web
application so as we know there different idea is that you can work on but I
will be preferring PyCharm because it is very easy and it is very reliable so
let me open my pycharm so this is my jetbrains by some community edition
so it'll just take a few seconds so now it's loading my existing project now I
don't want to see my existing project inspired to create new one so there are
two methods to create it so either you can use a pychamp down there itself
or you can itself go to my files directory and create any directory and then
you can lower it to pie chart so I'll show you both ways so let me go to my
file directory and let me see where I can place a new folder settle create a
new folder here with the name ed Eureka so now I'm inside my head
Science folder now what I can do here I can either create a fold over here
and name it as a Django project or what I can do I can just simply go to my
pie charm open the terminal' from here and I can create a project using
terminal so for creating a new project you just have to type in this command
Django admin start project followed by the name of your project so let's say
the name of the project is my project or let's say first project so once you hit
enter it will just take two seconds and here as you can see your first project
has been created so once you go into it you will see another container with
the same name and one man is file so what is this so let me just go back to
my presentation and let's see what exactly these are so as you can see have
already created this project first project now
ideally the directory where I have placed this project it somewhat looks like
this so it has first project so it was containing two files which was managed
at me by file and the first project and inside first project they were certain
file so let me go back to my files and if I click on first project it tells me that
there are more files that is in it settings URLs and WSGI so you can go
back to presentation and let me explain all these files so as you've already
seen this is my project structure so first project is the name of the project
that I've just created and inside that Django has automatically two files one
is managed or py and the other one is first project so inside this first project
which is a container that is provided by Django with the same name and
side of the project it basically contains some set of default files with it
which is in it that py settings URLs and WSGI so let me go into detail and
let me help you understand the structure of the project so as you can see in
the left side so this is my project structure
So this is the name of the project that I've just created so you can rename it
to anything you want to now inside this
I have one loop container with Django automatically creates with the same
name as out of the project so this is basically the actual Python package
which contains some default files now what are those default files it is
image settings URLs and WSGI so let me go one by one and explain you all
of these now in it tells python that
this is to be created like a packet so Django has some different rules that
you need to follow next is your settings
now this file contains all your settings or the configurations of your website
so again this is kind of your main hub so inside this you have an option
called as installed app where you have to add the application that you will
be creating so this is one of the most important things that you need to do so
every time you add an application you have to manually add it here also
there are some other stuff like secret key and all that so we will go into
details when you actually see this settings.py file next is a URL file
so this is kind of your main control of the website which basically connects
all your links of the project with the view next is a WSGI file
So this is basically for Douglas J compatible web services which acts as an
entry point to serve your project and finally towards the end we have
manager py file that
basically interacts with your Django project so this is all about the structure
that Django follows so now let us go ahead and go to my pie chart where
we start building our own application so I already inside my first project so
now let me just open from here so I go to the same location and going to
Yoshi then Eddie a car so this is where my project is so I just load it up
from here so as we have already seen this is my project name it has two
files at its project first project with the same name and then the manage.py
file that interacts with the jungle so once we go into this project we have in
it settings URLs and WSGI that we have just discussed
now what is the first step that we have to do so now we have to create an
application so before going to create an application
let's go to my server and let's run my server so for running a server you just
have to type in pipe and manage dot py and runs over this is Dan so let me
just go to my google and type in localhost so here you can write your local
IP or you can say the local host which is not accessible outside your home
network
it is only accessible inside your network so now it says that it worked now
let me go back to my pycharm and let's see how you can create an
application and display a view to it so now for creating an application you
just go to a terminal and just type in Python managed or py start a panda
name of the application
so let me name it as first application or stop so as you can see here we have
some extra files added to this up now what are these so in it we have
already discussed then is the admin so admin is basically for administrator
so I guess I've already told you that Django itself provide an inbuilt admin
panel so we'll look into that after some time and then is your apps folder
which basically says that this is your configurations and the name of the
application is firstapp for now then is your models
so models is nothing but it contains your data base and submitted it over
here then is your test
so test you can just leave it for now because right now we are not creating
any test cases but yes in the further sessions we'll be creating our own test
cases as well and last is your view
so let's just check this once again so what you've done till now we have just
created a project so inside this project we have you our insert py file which
is the main controller so here we already have admin URL I have included
one more URL that is full staff that URL so this URL we have created by
ourselves
so I'll go to this firstapp that URLs so this is my first app I go to URL
so here if I go to my local host so here it says that Jiang who tried these
URL patterns in this order
which displays the view next let us see what are the different components or
you can say the building blocks of Django
so let's just start it with the very first building block that is you're wearing
now ORM stands for object relational Mapper so what happens exactly
ORM. ORM is basically you can design or you can define your own data
model so what exactly is a data model so whenever we talk about data base
or when we talk about the data or related things ORM comes into the
picture so it's a code library that automates the transfer of data stored in
relational database tables into the objects so if you have say here first of all
get imported models and then we have created a class called as employees
now inside this class employees I have two pins witches firstname and
lastname so this is what a model is storing so it firstname should be of
maximum 10 and lastname should be of Aksum length 10 and after that we
need this string method so that is why it is returning self dot firstname so
this is how we can play with objects and you can retrieve the information
present in any other database and you can see the results next very
important feature is URLs and views
so we have already discussed about URLs and views Django here
encourages a beautiful URL design so we have already talked about views
and how you can map a view with the URL so what we have done best
specified the URL pattern in the form of local expression and to say at
views on index. index is a function or you can say the method which is
defined in the view.py file and once we go to the view.py file if it says that
it has look Mathered index that accepts requests and returns HTTP response
so this is what we've already discussed in the basic application that we have
just created so I think you guys must be clear about these concepts and till
now guys if you have any problem or any kind of question that comes into
your mind just come in below this video and we'll be happy to help you
with the same so next component is your templates
so templates are basically your front-end or you can save your view has
been displayed it is for front-end developer who likes working with HTML
and everything like that so I think you must remember that in the
settings.py file I showed you this temporary directory so let me just go to
my PyCharm and just show you where this template directory is present so
this is what django provides
so you don't have to configure any template or anything like that django
does everything for you next we have a very important topic that is a form
so django provides a powerful form lighted now it handles all your security
or your HTML everything inside it so form is basically a collection of
elements which helps you enter the text it helps you select the options you
can manipulate your object and you can have the control all over it so as
you can see here I have created a form which has the fields first name last
name email and a button to register so basically this is a registration form so
let me go ahead and help you create a form in Django also in this form I
will be creating a template as well so that it will help you to relate how we
can create a view and how you can create a template with it so let me go
ahead and let me open my pie jump to create a form for you so now coming
back to a project we have view stop me by 5 where I have returned the
HTTP response now I will need this HTTP response now in fact I need a
template so for that I will return render which has a request then you
specify the part where a template is present and then we'll pass the
dictionary so now we have to create a form for this so what I do here I first
create a form so hey I'll be passing the big story that is context and inside
that I'll be having the key so let's say my key is my registration form and
farm is my instance so here as you can see this is my variable this is my
variable which is a class representation and then I have passed this context
which is a dictionary over here so this is my key and this is just the instance
of it
so what I have to do I have to create this index.html Basically where your
template is present so I first go to my first app then I'll create a new Python
file I can say html file
and hell I'll go to templates folder which has the same name as first
application and then my index dot HTML so this is my HTML format
that is forms
so first of all here we need some input so here I write from Django import
forms so this is a pre build thing in Django
so you don't have to create or write manually things so I create a class all
our registration forms so this is the instance that I am talking about now this
will basically allows you to access all the fields so inside this I will be
writing with fees so let's say I need first name I need max England to be 20
then I need last name and I need one more field that is an email field so I'll
simply write emails to form that email wait so now everything is done
to let me just go to my terminal let's see what happens when you run the
servo
so here it says that you have some unapplied migration so first you need to
migrate it so I just break the server using control-c so to migrate the project
I have to write Python manage.py migrate
so Jango literally grade it Auto really tells you what you have to perform
next or what is the problem you are getting the errors so once you click on
migrate it is migrating
so next if I run this project
so now the red signal has gone so this will work down let me go back to
terminal again and let's see what happens
so as soon as you click on the refresh button your form will be displayed
so here you have first name last name and an email ID so here in this first
name field if I save type a value that is Mahesh let's say the last name in
Kumar and any email ID that is let's say
[email protected]
now but we don't have any button or from where we can submit this
registration form so let me go back to my code and let's see where we can
add this so this is my views.py file so this is my template that is index or
HTML now here to add a button to it you just have to type in input type
which is a submit button and we can give a value or you can say name such
as register
so now when we go to this forum we have a baton as well to register
so here if we type in any values the data will not get ingested into it so why
this will not happen because we have not carried these are models for it and
from where you can create data models you have to create a separate file
that is a model store key by and then you have to create different objects for
it from where you can retrieve it now if you want to play around with this
registration form just go back to your code and just write but as under scope
P
so let's see what changes does it go so here the view has been changed
if I do as_table again
so this is how you can play around with the templates so let me go back to
the presentation and let's see what else is left with them so we have covered
forms next we have authentication
so Django comes with a fully featured and secure authentication system so
Django handles user accounts the group's permission and all the cuckoo
based user sessions so now if you see at this code it already contains the
earth which basically represents the authentication so this is how Django
accepts take care of that authentication part next is your admin
so admin is the strongest part or you can save the main USP of Django so
Django provides an invade administration panel so this basically it takes
your data which is pink period in the models and then it will retrieve you so
this is how my admin panel looks like you have a user name and you have a
password so let me go back to my code and let me show you how exactly
an admin panel looks like so if I go to admin
so this is my username and password for now I don't have a username and
password so let me go back to my PI charm and let me create one for you so
i come out of the server first and for creating a super user you just have to
type in python managed or py create super user so here it will ask you for
username and it also gives me an option that we blank if you want to use
the same username that is Mahesh
so I will just leave it blank so it asked me an email address so it's just an
optional thing so the password it is very important so let's say I give a
password I again ask me a password so I've given a password which is too
short so let me just give a better password so here my super users created
successfully
so now I will go again back to server both let me just run my server
so now I have a username which is Mahesh and I've just set my password so
let me just type in here
so you just have one user which is IO XI so if I want to delete it you can do
anything in this admin you can add to the users you can select them with it's
an admin or it's not an admin so you have raised options over here we can
also change the side we can logout and many other things so let me go back
to the presentation and let's see what else we need to cover so we covered
the administration part so this is how a Django provides a suite admin panel
so now let us move forward so next is internationalization
now internationalization basically translates your text in different languages
it doesn't care about locate specific formatting doesn't care about the time
numbers irrespective of the timezone so choose the home page with the
welcome message that is translated in a user's language so this is how
django keeps on winning hearts next we have the last feature which is
security
so I have already discussed some extent of security so django provides
multiple protection now it provides protection against clickjacking CSRF so
CSRF you already discussed in the forms so we have implemented this as
well they provides protection against experience action and remote code
execution so this was all about the building blocks of django let us move
forward with the
Python Django Project:
A web application project so here I have just create a demo website which
displays all your courses the description along with the date and time
backed up with the database so in this I have used few of the comples that
we have discussed and some of the advanced stuff so let me just showcase
the project view
oops I guess I have not improved with the manage.py file so let me do it
again
So this is how my website looks like now here I have some static files and
some text displayed so here is my Python Django tutorial which is the
header of the website and here in the left hand side I have their - reiga logo
just below that I have a navigation bar which contain three tabs that is
Home tab courses tab and contacts tab after that in the same div container I
have placed four static files which says Science has live classes it has
export instruction it has to enforce and support and flexible scheduling and
towards the end in the footer I have given contact you can contact the
Science record support or educator for then we have Facebook the
Facebook page of Science opens up next we have YouTube so here I've
given the link which opens up the Science channel so this is how a website
looks like now if I go to home it will just say hey we will come to in
Science but if I go to courses so I do ever provide a lot of courses so here I
put a slider
37:31
which changes automatically with the number of object that it has so right
now I just have Django and Python so if we click on python
so python and the course subscription is below and when it was published
it's all written it's the date and time has been mentioned similarly if i go to
courses again same is case with django
django it displays
the description and the date and time with respect to it last we have contact
so encounter I have just displayed the view which says contact in support
and any record code which basically gives 24/7 support so this is how my
website looks like now in the courses tab I just have two objects slamming
go to admin page
and let's add few more courses so oily creator username for this project so
the user name is Mahesh and the password and specifying
it has post
now here it displays just two objects now here I can add unlimited courses
now let's have to upload a new course which has a name let's say appear
and in description I just lied this is a new course so then you can just
provide the date you can select today and you can select the time is now and
then you just simply have to save it
so now as you can see the post has been added now
once you go back to your website he will see the course will be added well
just refresh this page where as you can see RPA is been added now
and I'll see the settings or you can say the URLs
where the user has been hitting so there are three URLs the first is the
admin which is by default secondly I have created my dot URLs and then I
have create a separate application which is courses and I've given a separate
URL that is course is not URL so these are the two applications that I've
created and I've given a separate URL to it so now let me go to my first
application that is mine so I'll go to my application
I go to URLs.py
so here I've defined my URL patterns now we know that you are your
patterns are in the form of regular expressions so this is my home page
where we have a view where we have an index method which must be
placed in view and we have given a name space to it second we have
contact where again I have here a contact method and given a namespace to
it so let me just view these two methods in the views.py file
so here these two methods are defined so the very first method is index so
here we have again not returned the HTTP response so here what we have
done we have simply returned the templates so what I have done up I have
created a template which is home dot HTML and there I have passed
dictionary which has a key post and then it has post 1 and post 2 similarly
contacts I have created one more HTML that is basic to HTML and then I
have just passed a dictionary which has a kiosk content so this is my
dictionary and then I have passed the content to it which says contact
[email protected] so these are my two templates which says home.html
and basic.html so let me just go to my template folders and let's see what
exactly is their home and basic so as you can see here
it first extends header dot HTML and then it will just display this text that is
hey welcome to Erica so this is present in the ginger logic now if I go to
header.HTML which it has extracted so let me just go to header.HTML
which has a diff container so if you guys are familiar with HTML so you
will really catch up this soon so inside this sub page for static file and the
text accordingly so here I have given the source of the image I've cleared
one more folder that is static and inside static I have my application then
you have folder name as image and then the name of the image so if you
see here I have a folder which is static then it has the same name as that of
my application and inside this I have already told you that I've used some
CSS
so this is the bootstrap that I've already downloaded so it's up to you you
want to use it or you don't wanna use it so just to represent this better I have
just downloaded the bootstrap and then I have just pulled over here so I'll
just show you from where you can download the bootstrap so I go to
Google and I'll just type in bootstrap
so this is the website from where you can download a bootstrap
so he'll it has a lot of things that you can practice so he'll say is download
bootstrap you now know the source and many other things are there now if
you see an examples you can find a million of examples over here so what
I've done I've just simply downloaded this bootstrap so this is a zip file that
comes with us so what I've done I've just extracted this folder and just paste
it over here so this file contains a CSS your phones and your JavaScript so
this already contains a lot of things
it has CSS it has themes and many other things now the one thing that I
have added is the image folder
now here if you see I have added these fixed images in the website if you
click on that so this is the first image that I've added to the website
let me show you where this image is exactly on the website so this is the
first image which has a name of life classes so this is my first image
similarly there is second image
so these are all the static files that are uploaded to this website so here in
my div tag I have given the source
a static my image and the name of my image similarly I have done it for all
the images next I have created a navigation bar
which is there in the left hand side so this is my navigation bar which
contains three tabs that is home courses and contacts so this is my home
courses and contacts so here I've used the list which goes to different URLs
at the same time so if I go to my home I have a different view which is
displayed if I go to courses I have different view and similarly with contact
so this is all about my left navigation bar again I have created one more dip
tag
where I have placed three static files so next I've also created one more dip
tag
where I have placed the slider so this is a slider that I'm talking about this is
code for slider and in the end I've just added a footer
so input I have just added the hyperlinks to it so if you click on 'contact a
new URL will open if you click on facebook a new link will open and
similarly with the YouTube so when my application is completed now let
me go to the other application which is courses so if you go to this URLs of
the main project
this I have already covered so next let us go to the courses application and
just hop into courses that URLs calm so courses I'll go to URLs.py file
so here I've created a URL pattern now here I've already told you that in
courses I have created a list so the list it collects different objects which is
created in a admin panel so here I have written a query set which says post
which is the name of my model and then we have selected all the objects
and then we have ordered by date now - it represents the descending order
and if you want to place a limit to it you can place a limit let's say 25 so
only 25 courses can come at one go next I create one more template which
is courses of HTML and if you need a detail description to it you have to
create another URL which has a PK which is a primary key now you will
not create a primary key Django does it for you so here it will represent
your first column in the model so here I have specified the model is my post
and here I have displayed one more template which is inside my courses
and this is post on HTM so let me go to courses and post at HTM so inside
my templates I have two templates
gentleman first go to my models so here I have paid one model now I have
clear class which is post I have different feels like course description and
date so this course has the maximum 140 description it has textfield love
this and similarly the date I have cleared one more method which basically
returns your self dot course so once your model has been created let me go
back to my courses on HTML
and here apply it for loop it says yet each object should come in the list so
here I have displayed the post date day it should come in this format to
display the year first and the month followed by the date after that I've just
given a hyperlink which says courses and it will fetch the post ID which is
the work of primary key basically it will just tell which column or which
courses it has picked up from so this is my slider next is your post of
HTML
so in this post of HTML I have just inherit the same header or HTML file
and inside this I have to display the course description and Dave now if you
notice here I have written post or body-safe and line breaks so if you go to
my website and if I go to inspect
so you can actually see this in different formats let's see if you want to see
this website in iPhone 6
as well
also if I close these tabs now similarly if I open my tab and if I squeeze
this window
it will not change anything so this is what these things do so this was all
about my project so I hope you get a simple understanding of what exactly
all these components and how exactly these components work so with that
I'll just wrap up my session so in this session here
first talk about Django famed book Ohio finval and Django is used after
that they've understood what exactly is Django and what are the different
features of Django once we got what exactly is Django if we move on to the
embassy enmity pattern the Django follows next we just got started with
Django and they appeared a basic web applications we just displays the
HTTP response after that we have gone to different components or you can
say the building blocks of Django and finally it was the end I have
showcased my website where I've implemented different components and
how those components work so this was all about Django so I hope you
guys liked the session now if you guys have any doubts or any query stated
to any of the concepts that I have discussed you can just leave a comment
and we'll fly to it at the earliest thank you so much have a great day
goodbye happy wicked I hope you enjoyed this.
OceanofPDF.com