Python Django Tutorial | Django Course | Intellipaat


hey guys welcome to intellipaat the web
is one of the most important frontiers for any organization in order to reach
out to a large audience base every organization has its own web application
which is why being a web developer is one of the most demanding career options
but how do you build a web application although there are many options out
there django is one of the most easy-to-use and versatile web
application development platform as django is written in python it is also
quite easy to learn even though it’s easy to learn it is also quite powerful
and power some of the most popular websites like instagram spotify big
bucket amongst others in today’s session we will learn how to use django for web
application development before you go ahead guys to subscribe to intellipaat’s
YouTube channel so you receive regular notifications on all our upcoming videos
now let’s go through the agenda we will start the class by learning how to debug
any Python application then we will learn how to use Python with MySQL
database then we will learn how to deal with XML and JSON data in Python then we
will have a quick introduction to Django framework then we will learn about
Django templates and forms after which we will learn about Django models then
we will learn how to create restful web services using Django rest framework
after which we will learn about testing and security using Django then we will
discuss some of the most important Django interview questions and their
answers these questions are aimed at helping you clear your next django
interview after which we will have an end-to-end project these projects will
implement everything you have learned so far to give you some practical and
real-world exposure at the end of the class we will also have a quiz so that
you can recap everything you have learned so far don’t forget to leave
your answers in the comment section below also guys if you’re interested in
intro and Django training do check out intellipaat’s Django certification course
the link is in the description below now without any delays let’s start off a
today’s class let’s start by understanding what exactly is a web
application so simply put a web application is a software that can be
accessed from a browser now what is a browser well a browser is
an application with which you can browse websites so we’ve got different browsers
such as Mozilla Firefox Google Chrome and Internet Explorer and with the help
of these browsers we can access a web application now for communication to
take place between any two entities there has to be a proper set of rules
between them for them to communicate so let’s say there are two people who want
to communicate with each other now for them to understand each other that have
to speak or that have to know the same language and that have to speak the
language with proper grammar and only then that be able to understand each
other and same as the case when it comes to a web browser and web application so
both of them should know the same language and both of them should follow
the same rules when speaking that language so these rules between the
browser and the perfect location are known as protocols so there are
different protocols such as HTTP protocol and the FTP protocol which help
the web browser and the web application to communicate with each other now a web
application or a web server can host different web resources such as a PDF
resource HTML resource and the JSON resource now a browser if it wants
access to these web resources then it would send a request to this web
application then this web application would access that particular resource
and in response would give that resource to this browser with the help of these
protocols so let me retreat this this browser by following those protocols
that could either be HTTP protocol or the FTP protocol it sends a request to
this web application and it basically tells the web application that so I want
this PDF resource so in response the web application accesses this PDF resource
and it gives to the web browser so this is how a web browser is able to
communicate with the web application so now that we’ve understood what exactly
is a web application let’s understand what is a web framework now when it
comes to most of the web applications most of their core features remain the
same that is most of these web applications require some sort of
indication so they need some sort of security and most of them require
database connectivity and so on so there are some core features which are common
to all of these websites now let’s say you’re building a website from scratch
and you would have to build everything from start so that would be a very
tiresome and a very cumbersome task you’d have to start off by giving in all
of the security features by yourself all of the database connectivity features by
yourself and that is a Herculean task so this is where a web framework comes in
so you can consider a web framework to be your collection of different API is
and classes which would help you to build a website very easily quick info
guys if you are looking for end-to-end training in Django do check out intellipaat’s django certification training course the link is in the description
box below now let’s continue with the class so web framework consists of
different features such as will make your life very easier so it provides you
automatic features for working with the database it will provide you templates
and it will also provide you security features so let’s talk about the first
one which is database so with the help of a web framework you don’t have to
establish the connection with the database all by yourself so the web
framework takes care of the database connectivity by itself and it’s a very
easy task now the web framework also gives you a
lot of templates so what exactly is a template well when a web browser sends a
request to the web application the web application has to return something now
when it returns it has to have a proper template so this is where different
templates come in so this is where you can actually choose how your response
looks like how your result looks like and then obviously your web framework
could provide proper security features hey guys it’s just a quick info so if
you are interested in doing an end-to-end certification course in
Django then intellipaat provides the python django certification course so
you can check out the course details in the link mentioned below so let’s
continue with the class so when you’re building a website there is a very good
chance that your hackers out there might add in malicious links into your website
so this is where a web framework provides you proper security features so
that it does not B’s by any unwanted malware’s or any unwanted
attacks right so there are a lot of web frame works out there such as you’ve got
django you’ve got flask you’ve got pyramid and you’ve got web two pi and in
this course we go to low and comprehensively about the Django
framework right so let’s understand some of the features of the Django framework
Django I know it’s not the most popular open-source web frameworks built for
python so with the help of Django you can build web applications in a fast and
simple way so it wouldn’t really take you much time to get started with django
right so let’s say if you’re building website from scratch and if it takes you
around 10 days to build a website on the other hand if you choose django and you
try to build your website then it would take maybe one or two days to complete
building your website so django has extremely fast to build your web
applications and the next feature of django is it is secure of clickjacking
and sequel injection so what exactly is clickjacking well as i have already told you when you’re building a website there are a lot of cases where hackers
might try to add some sort of malicious links in your website so this is where
django provides foolproof methods to secure for clickjacking right so there
would be absolutely no chance for a hacker to add malicious links into your
web application and also django provide security for sequel injection so what a
sequel injection now you guys also know that database is the most important
feature when it comes to back-end websites so there might be cases where a
hacker might want to get into the database which is connected with the
website so if he gets into the database then he’d have the entire access of all
of the users and their entire information
so this is where django provides security so that no hacker is able to
penetrate your database now django also provides scalability so what do I mean
by this so let’s say if you start off by 10 users when you’re building the
website but again your web application becomes a head and your number of users
keep on growing so from 10 users let’s say your number of users go on to around
thousand user now to have this capacity of thousand
users your website or web application has to scale up and Django helps you
with that and finally obviously Django helps in
database integration so with the help of Django you can connect with either my
sql or sql server so it becomes extremely easy for your web application
to be connected with the database right now let’s look some of the famous
companies which use Django framework so we’ve got Instagram Dropbox Pinterest
and Spotify so all of these major companies out there are using childhood
to maintain their websites right now let’s go ahead and actually install
Django into our system so the prerequisite while installing Django
needs to be that we actually have Python installed in our system so to see if
Python is installed in our system you need to go to command prompt and we need
to type in this command so python space – – version so we’ll go ahead and we
have to type in this command and if this gives in a python version then it would
mean that python is installed in our system elsewhere as not now after that
we’d have to go ahead and install django into our systems with this command which
is pip install django right now let me actually go to command prompt so I’ll
just type in CMD over here and let me check my python version so I’ll type in
Python space – – version right so I see that Python is installed and the Python
version is Python three point seven point four now to install django into my
system all I have to do is type in web installed Django now since I only have
Django installed in my system I will not go ahead and click on enter right now
that we have installed django into our systems let’s go ahead and create a
first Django project so to create a new Chango project we’d have to use this
command over here Django admin start project and then we’ll have to give in
the name of the project so over here the name of the project is a website so when
we go ahead and run this command this will create a new folder from where we
start at the command prompt now if we go into this folder website
this would comprise of another folder with the same name as the root directory
so the name of the root directory was website and inside this there’ll be
another folder with the name website and this folder comprised of all of these
files will have an init file a settings dot py file a URL s dot py file and WSGI
file right now let’s actually go ahead and create a Django project so let me
type in the command so the command was Django – admin after this
I’d have to type in start project and then I’d have to give in the name of the
project so the name of the project is website so I’ll click on enter let me go
to desktop and see if this has been created
right so over here we see that there is this new folder with the name website
I’ll go inside this and as we saw in the PPT over here we have this folder
website and the name of this folder is the same as the root folder and there is
another dot Py file with a name manage and I’ll go inside this now this has all
of these files over here so that is any dot py selling store py URL s dot py and
WSGI dot py now let’s actually understand all of these files and
PyCharm over here so starting with any dot py so this file basically make sure
that our folder is treated as a package right so this init dot py this over
here make sure that this website folder is actually treated as a package and not
just as any other folder then we’ve got the settings not py file so the settings
dot py file comprised of different settings so we’ve got the security so
we’ve got the secret key over here to launch our app and then we’ve got
different settings so these are the settings for the installed apps over
here so these are the different installed apps and then we’ve got the
middleware for the security purpose of that we’ve got the list of templates
over here so these are the different settings and the settings dot py file
and then we have this URLs dot py file now every website or every web
application has different URLs so when you open a website and you scroll
through different tabs of it what you’re basically doing is you’re
scrolling through different web apps or you’re scrolling through different URLs
over here and this what you see are the list of
the URLs which are present in that website so let me actually give an
example of this so over Here I am at this site envelop a.com blog and this is the
URL now what I’ll do is I’ll open this first link right over here this is the
next URL over here what is natural language processing now similarly let me
open this tab over here so here the URL which you see is ETL tutorial for
beginners now these are just URLs which are given inside this list over here
right so this is why the URL s dot py file is used to given the list of all of
the URLs of your resources and then we have the WSGI dot py file
so this basically stands for web server gateway interface and it is used to
establish a connection between the web application and the web server so for
that communication to take place properly WSGI dot py file is used and
then we finally have the manage dot py file so this is actually used to run the
server and add different users to the web application so now that we’ve looked
at the core files of our project let’s understand about URL mapping and views
so let’s see if there is a match of that resource in the URL s dot py file then
the URL s dot py file would call up you now this viewer is nothing but functions
or classes which speak o web requests and return a web response now let’s
again understand URL ah so we’ve got a user who requests for a resource from
this browser over here now that resource could be anything so it could be your
tutorials page it could be a blog page or it could be any other page so this
request is sent to this URL s dot py file so the URL over here is tutorials
dot URL s so if it is found and this URL s dot py file then it will invoke a
function from this view and this view would respond with that particular
resource to this browser so this is what basically happens when a browser sends a
request to the web application quick info guys if you’re looking for
end-to-end training in Django do check out intellipaat’s Django certification
training course the link is in the description box below now let’s continue
with the class right so now we’ll go ahead and create
our full django app right so we have to open our command prompt and go to the
same directory where we created the project and we’d have to enter that
directory now after that we’ll type in dir just to make sure that we have the
project name in the managed dot py file and then we’ll use this command over
here so the command s Python managed dot py run server and this will put up our
webserver right and this is what we get so we’d have to use this link over here
go to this link place it in our browser location and we’d have successfully
launched our project all right so let me actually do this so let me go inside the
project so I’ll type in Cd website now let me type in dir over here so I
see the name of the directory which is website and I also have the manage dot
py file now I’d have to launch the server so to launch the server I’d have
to type in Python and then I’d have to give in manage dot py after that I just
have to type in run server hey guys it’s just a quick info so if you are
interested in doing an end-to-end certification course in Django then Intellipaat provides the Python Django certification course so you can check
out the course details in the link mentioned below so let’s continue with
the class so this is where the project would be open I’ll copy this over here
and I’ll paste this I’ll click enter right so we have
successfully launched our project so we see that the install work successfully
congratulations now let’s say I were to add a new web application to my website
over here so what I’ll do is I will go inside this project over here so I’ll
just open command prompt and I’ll exit this so to exit I’d have to press
control C now I’ll open another command prompt
so from over here inside this project I will type in CMD and I’d have to create
a new web application so to create a new web app this would be the syntax so I’ll
type in Python manage dot py after that I’ll type start app and given the name
of the app so let’s say the name of the App is tutorial right sure successfully
created this new folder inside my parent directory right so initially we did not
have this tutorial folder over here after running this command Python
managed dot py start app tutorial we have created a new folder with the name
tutorial now let me also open PyCharm so you see that we have this new folder
over here which comprised of all of these files but if you look closely one
file is missing from this tutorials folder which is the URL S folder so I’ll
go ahead and have a copy of this so let me go to the parent folder over here
right so let me open website I’ll copy this URL S folder and go inside the
tutorial folder and paste it over here now I have the URL s dot py file inside
my tutorial folder as well right so now I’d have to create a new web application
where I actually want the web page to just show this text this sparked up so
let me see how that can happen so let me start off by going into this URL s dot
py file so this is what we have over here now this is the initial URL now
instead of this what I want is I want this to print something so I will type
in path over here and since this would be returning from the home page I’ll
just put in same quotes over here so if I put in single
quotes as the first argument this means that I am returning something from the
homepage and then I’d have to add a view as we saw in the previous slides for the
web application to return something it needs a view so I’ll type in views dot
and then I’ll given the name of the function which would be home so I’ll
type in name equals home now after this we also have to import views so I’ll
type in from all import views all right so all of the errors have been removed
over here now I’ll remove this one over here so this what you see is just a list
of all of the URLs which are present in this web application right now after
this we have added this view over here but we don’t actually have this view
inside this view dot py file so let me go ahead and add the function so I’ll
type in def and then I will name the function as home because in this URL s
dot py file have given the name of this view as home alright so let me go to
this view now since we know that a view actually takes per request from up web
browser right so web browser sends a request to this view and this view in
response returns something so I’ll just type in a request over here and inside
this I’ll give in the body so this has to return something
but since the request is in the form of HTTP the return or the response also
needs to be in the form of a HTTP protocol so I’ll use the function HTTP
response over here and inside this what I want the home page to return is this
text so this is sparta and this is what the home page will show when I open it
right now again we have this error over here because we actually have to import
this through I’ll type in from Django dot HTTP import HTTP response right so
this is also done now the last thing which we have to do is we have
the changes in the URL s dot py file of our web application but we also have to
add this link in our main project over here right so I’ll go into the URL s dot
py file of my main project and inside this I’ll again add a part and over here
I’ll put in single quotes over here so this would mean that this is the main
page and to this main page I have to include the tutorial’s Page so include
tutorial dot URLS so I will put in a comma over here and I’d also have to
import the include method so I’ll include this right so we have everything
set up now what I’ll do is I’ll open command prompt again run the project and
see what happens so I am inside my project over here so I’ll type in Python
manage dot py run server right now let me copy this over here and let me
paste it again so we have successfully created this web
application where our homepage shows this text this part
now just to practice let me actually create another project and another app
over here so this time I will name this project as sparta so we all know the
command so that would be Django – admin start project after this I’d have to
give in the name of the project which would be sporta so let me go to
desktop right so we have successfully created this new folder with the name
sporta I’ll go inside this and this is what we have over here so we have this
manage dot py file and we also have this sporta folder and it comprises of these
four files which we know what exactly are right now I’d have to go ahead and
create a new web app inside this so I am over here now let me go ahead and type
in CD sporta now let me type in dir so we see that
this man is dot py file and we also have this sparta of holder now inside this I’d
have to create a new web app so I’ll type in Python and then I’d have to give
in the manage dot py file after this I’ll type start app and given the name
of the app so the name of the app is let’s say thirty so I’ll just hit enter
now I have created this new folder so T which comprised of all of these files
now again this does not have the URL s dot py file so I’ll open the Sporta
folder I’ll copy this and I will paste it inside this thirty folder over here now
let me go ahead and open this with pycharm so we have this folder over here
so this is our main folder and this is deep web app over here and this is the
URL s dot py file of the main project and this is the URL s dot py file of our
web application now I’d have to add a URL over here so I’ll type in part and
since this is being redirected from the home page I’ll just put in single quotes
over here and then I’ll have to type in the view which would return something so
view dot and this time I’ll just name it to be sporta again and after this so
this actually has to be views and not viewed and then the name as well I’ll
give this as sparta right so over here I’d have to import the views method so
from all import I’d have to type in views quick info guys if you’re looking
for end-to-end training in Jango do check out intellipaat Django
certification training course the link is in the description box below now
let’s continue with the class right so we have made the changes to the URL s
dot py file of this web app now I’ll go to the views dot py file and let me
create the method over here so def and then I’d have to given the name of the
view which would be let me just confirm over here so the name of the view is
sporta so def of Sporta and speaks in a request over here so I’ll
type in request and this would return something and the return would be in the
form of HTTP response so I’ll type an HTTP response and the response would be
we are 300 now let me go ahead and import this from Django dot HTTP port
HTTP response right so we have also made the changes in the views.py file and
we’d have to make the final change which is in the URL s dot py file so over here
I’d have to import the include method and also add in a new path over here
right so now I’ll have to include the final path over here so let me put in
tab over here will be path I’ll put in single quotes over here and after this
I’d have to use the include method and I’d have to include thirty dot URLS this pretty now come over here right so we have successfully created
this web app now let me go ahead then run the manage dot py file so it will
be Python manage dot py and I’ll type in run server let me
copy this link again and place it over here right so we have successfully
created this web app where it states we are 300 here we will discuss the capstone
project for django self-paced course this project encapsulate all the
concepts of django that we have already discussed so without any delay let’s
take a look at the agenda first of all we will build a web application using
Django web framework then we will extend a web application with REST API
capabilities using Django rest framework all right now let’s see what are the
softwares that need to be installed in our systems
first of all Python should be there django should be installed and then
Django rest framework should be installed as well for managing the
database we will be using my sql and last but not least you should have
postman or insomnia to handle the HTTP requests so let’s start with the first
part of our capstone project here we are supposed to build a web application
using Django web framework where users can see and post their thoughts the
first requirement is the web application we’ll have a dashboard or home where
latest posts will appear second requirement is in order to post thoughts
one has to be a registered user so that means we need to create a registration
form third requirement is user needs to be logged in in order to see their
profile in the navigation bar so that means we need to create a login logout
system as well fourth requirement is user can post new
thoughts from their profile only so that means we are supposed to create a
profile for users fifth requirement is from database latest post will appear in
the dashboard and the sixth requirement is users should be able to log out once
they are done and that is part of login level system itself so yeah
let’s get our hands dirty and let’s start with the project alright let’s go
ahead and create a new project we’ll head on to command prompt will run the
come on Django admin parte project start the project and we’ll name this project
as project hit enter so this command will create a project directory inside
the directory that we’re running – command prompt from so yes this product
directory has been created let’s go back to our command prompt and
go inside this product directly father yes we are in fight project retreat and
now you’re gonna start an application and let’s say the application is called
o between VR made application and we’ll run this command I to managed at the bar
we start the path and enter all right so let’s go back to the right three years
home application has been created for us but before moving ahead let’s go inside
this directory and as you can see one file has been messenger which is URLs
dot pi and let’s go inside project directory and copy and paste is file
over there now in order to perform further
operations I will pie chart alright before moving at I have to perform some
changes inside this settings dot PI file so I will be changing the database from
the default one which is our synchronize ring to my sequel and then I have to add
database name here in place of the default one let’s keep it project and
then I have to add the user which is my flute and then I have to add the
password here as well here password is empty so I’ll leave it blank and then
I’ll add the host which is my local host and then I have to add a port number as
well which is going to be three three zero six
and last but not the least I have to add one option here so that I can prevent
data loss from my database and it goes like this option and inside the
dictionary add you set sequel mode to strict turns table okay and yes
so this is our configuration for my sequel database we have to perform one
more configuration here since we have created a new application called home we
have to let Django know that we have a newly created application in our project
and in order to do that we have to add it inside the installed apps list and
let’s edit here yeah and they are good to go and before we run our server let’s
go ahead and create the database that we just added here and let’s open jump and
run Apache server and my sequel let’s click on admin you can leave you to it
local host PHP my admin page where you can create a new database and let’s move
it as project let’s create it alright so we have a database here called project
and now let’s go ahead and open command prompt and let’s run our server runs
over hit enter and yes our server is running in this address and let’s go
ahead see how does it look we will be building our web application
in this web server so let’s go ahead and start our development process first of
all we’ll be setting the URL spot so that we get to see the home page of our
web application when we land on our jangles development page alright let’s
go back to our project and let’s go inside projects URLs dot PI first we’ll
do port included here and then we’ll set that warrants but for our development
page we have to keep it blank will include so
here will be including home applications URL so basically this command will point
to the home applications URLs dot PI file and there we have to
add another URL path so that we get to see a view when we land on the home page
but before we add a view here we have to import the views inside the views dot PI
file so let’s go ahead and import from dot import views all right now we will
add on view let’s say we call it home we have not created any views yet we have
to create it afterwards and let’s name it home alright now let’s go to our
views dot PI file and we don’t have any busier so we have to create a view
called home alright and it will take a request of this and we will be rendering
template or a view that will show up in the development page so we will render
the request and it will return let’s say your template called home page dot HTML
and then we don’t have any context here let’s keep it blank all right so this is
our view but since we might have noticed here that we don’t have any HTML files
or any template files created yet so let’s go to our project and create a
directory here or our templates will store all of our
templates here let’s go inside the template and create a HTML file let’s
create a base file here from that base file we will be inheriting all the
templates inside our template folder so let’s create another HTML file let’s say
it or page page yes all right now let’s go inside base dot
HTML directory and we will add the tags here let’s say a block called title and
we will end the block here and block doctor sorry and block title and
similarly we’ll be adding one another block here for our content so block
content let’s keep it blank as up now and let’s end our block here alright now
let’s go to our home page dot HTML let’s remove this and let’s extend the base
five extends and kept place dot HTML all right now inside the title block block
title we want to add a title here let’s say
let’s end it let’s say post your thoughts post your thoughts and then we
will be having some content obviously and as of now let’s try a simple one
block content let’s end the block content here and let’s have something
let’s say inside a paragraph we will add hello everybody hello everybody all
right so we have created our templates inside the folder called a template but
Jango doesn’t know it yet so in order to tell Jango that we have
created another folder called a template inside our project and when you are
looking for templates you need to go and open this folder called template in
order to do that we have to go inside this projects setting start by folder
and make some configurations inside this templates we have to follow this format
let’s copy this and paste it inside and we will just name our folder which is
template all right so Jango can tell that it will look for templates inside
this template folder now let’s go back to our command prompt let’s run the
server again yes the server is working let’s go back to our command prompt and
run our server again Elliot’s working it sells and let’s
refresh it and there seems to be a problem here alright so in this file we
have uh syntax error let’s go back to our project to the side so let’s so
remove this space here all right let’s run our server again let’s go back to
browser and refresh it yes it’s working fine as you can see our title has been
displayed here and our content is being displayed here so let’s go back to our
project and start developing the web application that we are supposed to
build all right now let’s go ahead and create a navigation bar option inside
our web application and in order to do that we’ll go inside template folder and
create another HTML file let’s call it nav bar hit enter let’s remove this and
extend our base file here the base dot 60 ml 5 extends days dot
HTML all right that’s inside the block content we will add our navigation bar
so let’s just end the block here and no space and o’clock content and then
inside this we will create our navigation bar let’s keep it unordered
list and inside this we will have a list of options that we want in our
navigation bar for example home user profile about login registration things
like that as of now let’s create list and inside that we want
our whole option head up now let’s clip the HTF to plank let’s say we want to
display their about option as well in order to do that we have to create this
another list called about and yeah and then we want user registration form
option here as well and so in order to edit we will add the registration form
option here let’s keep it right let’s say let’s call it signup alright add up
now let’s see if it is working or not and let’s go back to our home page let’s
include the navigation bar html5 here so we will include bar dot HTML inside the
block content remember so we include our navigation bar option inside the home
dot HTML page so let’s go back to our server and refresh it yes you can see
our navigation list has been displayed over here and we will customize it but
we got to know that this is working but in order to customize it we have to
create a CSS file so but we will make this project structured and in order to
keep it clean we will create another directory let’s call it static all right
let’s go ahead and create this static file and here we will be storing all of
our CSS images or JavaScript files so let’s go inside this and create a file
in fact let’s create a folder called CSS so that we can
clean and all right now inside this I will create one file called bootstrap
dot CSS hit enter and since we are not focusing much on the front-end part so I
just add one simple CSS file over here and you can pause the video and watch
the configurations here alright so I will paste it over here and save it
alright now we have a folder called static and inside this we have a CSS
directory where we are storing one simple bootstrap CSS and in order to
tell Django that we have this static file inside our project what we are
going to do is we have to configure it and in order to do that we have to go
inside settings dot pi and perform one simple configuration I’ll just copy the
configurations here and I’ll explain it what’s going on so this configuration is
for Django to create one folder called assets which will be the static root and
this assets folder will be basically the replica of the folder that we have
stored our static files in which is static so and every time we modify the
static folder we have to perform one command in command prompt to update the
assets folder as well now let’s go ahead and save this and let’s go back to our
command prompt and let’s clear the screen and now let’s perform the command
that I was talking about by to manage dot fine collect static it enter as you
can see 20 static files copied to assets all right
now if you go back to the project you can see one folder called assets has
been created for us so inside this we have the CSS file and if you click here
you can see this is while here now let’s go back to our
command prompt and let’s run our server managed by and yes it’s working fine now
let’s go back to the browser and see if the changes are reflecting okay I think
we have made some mistake here all right let’s check here okay the link has to be
added inside the block content yeah let’s save it let’s refresh it as you
can see we have the navigation bar over here and let’s go ahead and create a
registration form user registration form so I’ll head back to my project now
let’s go ahead and create a new application called
users where we will be creating our user registration forms login logout systems
things like that so let’s go ahead and head on to our command prompt let’s stop
this server for a while and let’s create one application to start after let’s
make users it entered all right let’s go back to our project
yes users has been created for us now let’s copy and paste our URL Scott PI
from product let’s add on to our project and let’s open project copy and then
paste it over here alright let’s refresh it let’s go back
to our project losers dot PI has been added in the
users application as well so before moving ahead we have to add this newly
created applications inside the installed apps list let’s go ahead and
do that let’s hit enter and add our newly created application over here
alright let’s save it and now we will go inside
users views and we will create one view that will show the user registration
form so in order to do that I will import that user creation form from
Django total three oh my god country dot earth and we will import the
user creation form let’s create our view now let’s call it register and it will
take the request and alright so we want this view to render one form context so
in order to do that I will store my user creation form inside this instance for
you let’s call it form and let’s yeah let’s do that and let me turn first well
we are gonna render one template here as well so let’s name it register dot html’ now we are going to render the
context that I was talking about the form context so in order to do that I
will have to add it inside a dictionary and alright I have to add for render
here so let’s go ahead and create this template so that we can render this
inside our view and we will add it inside the template directory 15:05 the same name that is given over
there so enter did it all this and now we are going to extend the base file
over here distance based dot HTML entered now let’s load the static folder
as well and load static now we will add the navigation bar over here now let’s
start our block content then we will add the navigation bar over here now our next thing I’m gonna do is I’m gonna
start a div so that I can separate my form from the navigation bar – so now
I’m gonna add my form inside this directory let’s go ahead and create the
form I will start the form that here now this form will be taking post method
since we are using post method here so we have to add the CSRF total over here
in order to avoid the cross-site request forgery and the spoken looks like this
CSRF token now I’m gonna add the field set over here where I will be rendering
my form context inside this field set let’s go ahead and render our form and I
will be rendering it as aircraft so but before this form context I want to
display one simple text let’s say I’m going to add one legend over here and I
want it to display join now and let’s go ahead and save this but we have
only created the form over here but we have not added any button for users to
submit the form so let’s go ahead and create a button and let’s add the button
tag over here so I want this button also I want feet to be of type submit’ let’s
go ahead and write the text I want inside the button let’s say sign up and
yes let’s save this and let’s go back to our URLs so we have to add one URL for
this view that we have just created for user registration form now let’s go
ahead and add one URL path for the user registration form that we have just
created let’s head on to users and and let’s remove this and in place off home
dot URLs I will just add the view that I want to be rendered over here view
alright let’s import the view first not import so I want the register to be
rendered over here let’s name it register alright now let’s go ahead and
add the URL path that will be pointing to our users URLs dot five so we have to
go inside projects folder and we’ll be adding another URL path over here for
our register form so we will add registered and we will include the user
we will include the users dot URLs folder and let’s save it alright and now
let’s go ahead and create the user registration form and the login logout
system and that I will head on with users view dot
PI and I will import let’s go ahead and create user registration form and first
of all I will create one new pite and find over here that so it forms and
inside this I will add my registration form first of all let’s go ahead and
import forms from Django let’s put form now I will be importing
user creation form and basically Django country dot hot dog forms importation
form so basically this user creation form will have three fields username and
password one and password too but it does not have the emails field so in
order to do that we will create one class over here let’s name this new
class as user register form and we will be inheriting it from the user
Krisha file let’s now we want the email field over here throw in order to do
that I will add this new instance called email and I want my forms to have email
field all right now I’m gonna create one net a class over here
Netta so it’s gonna have the model which is built in user model so I have to
import this user model before I can add it over there and in order to do that I
will import from general country Earth model import user so that’s important
and we want the fields such as user name email password one and password to to be
displayed over there so I’ll mention it over here is equal to I’ll add it inside a list so
username and your email then password password to public let’s
go ahead and save it let’s go to our bills where I will be
creating the viewport user registration form let’s go ahead and import the form
that we have just created dot forms import user register form alright now
let’s go ahead and create our view let’s name it a register it will take a
request and yes if the form request is post then we will perform some
operations such as checking if the form is valid or not and if it is then we are
going to save it and we want Django to redirect us to the login page
once we submit the form so if the request method is post then I’m gonna
check the validation of the form so in order to do that create one instance for
instance using the user as perform and now I am ready check if the pump is
banned or not from God is valid then I am going to save if the form is valid
all right now I want Django to redirect us into the login page once I submit the
form so in order to do that I will just enter return redirect but before I
redirect it I have to import the redirect function here so yes yes
alright now it’s working now let’s go ahead with the condition else if the
request method is not post we want the Django to display us the same form so
let’s say form and I want the user register form and I
want to return the same template and let’s go ahead and sister dot HT note
and I want to render the form instance over here all right I have to remove one
M from here all right now I have my view for my
registration form now let’s go ahead and create this register dot HTML template
and I also don’t have a login page I will also have to create it before I run
my server let’s go ahead and create a template now create a new HTML file
let’s name it register hit enter let’s remove all of this and extend our base
file over your things base dot HTML it entered and then I will be importing
the static files I will be loading it load static and then I will start my
blog content then write and my block and block alright now inside this I will add
my registration form but before that I want a page to display the navigation
bar as well so let’s now let’s go ahead and add the navigation bar include bar
dot HTML all right now let’s go ahead and create the form I want the form and
the navigation bar to be separate I don’t want it to overlap so I want to
create a new gift for my form so I just start it alright let’s go ahead and
create a form here so my form is going to take the method post method and since
we are using a post method over here we have to add the token CSRF token in
order to avoid the cross-site request forgery so we will add the broken over
here C as our F underscore token and yeah let’s go ahead and start our fin
set and this will contain my farm so I want my form to be
this plate over here let’s uh display it as Telegraph and also I want to display
one text inside this pill set let’s add a label over here let’s say join oh all
right now let’s save it but before we move ahead we will add one
style here so that the navigation bar and registration form don’t get overlap
so I will just add the styles and let’s say I will add one margin top let’s say
80 pixels and I want the weight of the form to be around 900 pixels and the say
I wonder margin set shows all right now let’s go ahead and save it now that we
have the template as well as the view for our registration form let’s go ahead
and create the URL we will go to users dot URLs dot pi and I will just remove
it so I want to render on view over here my views all right I have to import my
view before I do that from dot import these you know I can import my register
view and I will just rename it register all right let’s save it and I will go
ahead and add one URL path in my main application which is my project
and I will go to our else so that this URL will lead our map to the users URL
dot pi folder so let’s go ahead and do that register and I will include the
user URLs folder all right let’s save it or I have to add 1 comma
over here and now I can go ahead and save it all right then let’s go ahead
and run our server and restart it it’s working fine
let’s go the browser let’s refresh it and now let’s head to register okay it’s
throwing an error here let’s check what the Edit is all right let’s fix it in a
second all right let’s check our forms yes we have to meet a mistake here let’s
fix it then let’s check our view we need to add one requested method here let’s
add the requests most here and I have two here so let’s save it and let’s go
back to our command prompt let’s restart it okay and let’s refresh it yes
our registration form has been displayed over here and but have you noticed one
thing we don’t have a submit button here and our registration form does not look
pretty but in order to do these things let’s go back to our project first of
all I will input one package called crispy forms which will enhance the user
interface of the form so let’s go ahead and install it in order to do that I
will run this command it will stall Django recipe Holmes hit enter it’s
gonna take a few seconds to install and yup we have already installed it so
let’s clear the screen let’s go back to our project and I will import the crispy
forms inside my register template let’s go ahead and report it load crispy homes tanks I will and I
will remove this and add the filter over here as crispy
all right I have added the crispy tags filter here
but I need one more thing in my form I need the submit button so in order to do
that let’s add button here let’s start one div I will start button tag here I
want this class to be and I want it to be of type submit’ so I want the sign up
takes over there to be displayed let’s add it let’s save it and then let’s
restart our server 2.5 all right that’s working fine let’s go back to our
browser let’s refresh it oh it’s throwing one error let’s see load crispy
forms tag all right so load crispy forms tags ok we are using – not underscore so
that’s why it’s showing an error let’s fix it and then restart the server let’s
refresh it another error all right so load crispy forms tags so this is
throwing one soo text F all right the problem is we have not
registered the crispy form Stags is in our installed apps so let’s go back to
our project and open settings inside this list we will add crispy forms
crispy and I’m gonna save it let’s go back and restart our server it’s again
true only one error but let’s go back and again underscore here let’s save it
and let’s restart our server it’s working fine now let’s go back and
refresh it yes our signup button is being displayed over here and you must
have noticed the form looks better now so alright we have the registration form
let’s try and giving an input over here let’s say the depart example.com all right once we submit it that’s
throwing one error because we have not created a login page yet and we have
mentioned our project to redirect us into the login page once we submit the
registration form so let’s go ahead and create the login logout system so yep
all right let’s go ahead and open our project let’s go inside
URLs dot pi and here we will import the login logout view that we have already
built in in Django and first of all let’s just import the views from Django
dot country God views so we don’t want these views to be messed up with other
views so let’s import it as thought views and let’s add the URL paths now
let’s say for login then we will add our views here I will call the login view
and we want it as view and I’m gonna render one template over here and I’m
gonna add the template me as login HTML dr. Kristina all right now one more
thing at the name to me blogging all right now similarly I will do the same
thing or logout all right let’s play secret logout then
a log out view then just go to our logout HTML and this is going to be
loved alright but above now I don’t have any template is called logging all oh
god so let’s go ahead and create these templates and I will go inside my
template folder create one HTML file called it noggin
and I will create another HTML file log out let’s go ahead and create our login
dot HTML page first of all I will extend the base file here you know alright now
after this I will add the crispy form tags
load all right thanks let’s load the static folder as well alright let’s go
ahead and start our block content let’s end it over here alright now I will add
my login form here let’s start with deve and I will start my form so this is
basically the same kind of form that we are using registered so I’ll just copy
this and paste it over there yeah so let’s just copy and paste the
style from here and let’s add the navigation bar here let’s include all
right now I have my navigation bar over here I have my form and my form is
taking the post method and I have added the CSRF token form over here as well so
yeah so instead of sign up I’m gonna say log all right now let’s save it let’s go
to logout now let’s clear everything here let’s extend the base file base dot
HTML let’s include the block content block then over here let’s just end it
right away log tilt now I’m gonna add the
navigation bar over here close it Ned disco ba 60 left alright next thing I’m
gonna do is I’m gonna display one message over here and
this is just gonna say you are logged in let’s edit inside okay let’s save it now
before we start our server let’s go ahead and do some configurations inside
our main settings fortify folder we have to add one contribution here and let’s
copy and paste it and I’ll explain what’s going on over here so basically
here we are telling you django to use bootstrap for for Krispies template back
and in these two lines we are saying that login page once the login form is
submitted we want Django to redirect us to home and login URL is defined here as
logging let’s go ahead and start our server now let’s stop it and clear the
screen and restart the server enter alright everything is working fine let’s
go ahead and start from the register page alright we are in the register page
we are going to create one user let’s say until 1 p.m. is until one after it
some let’s create one password say it entered so we are in the login page
now as you can see but we will change this legend and all right let’s just try
and log in with the user that we have just created hit enter yes we are in the home page
now we are logged in so now we want the navigation bar to display logout and
user profile option over here so that we get to you know create our post from
profiles and also once we are done we can log out from here itself so let’s
get back to our project and now basically we are gonna create one user
authentication system so when the user is going to be logged in the navigation
bar will display options such as Oh about logout and user profile not sign
up and log in because user is already going to be logged in right and when the
user is not logged in we want the navigation bar to display options such
as own about sign up and login option so basically that is our requirement and
let’s get back to the project let’s open the navigation bar dot HTML and we will
make some changes over here so basically we want more navigation bar options over
here let’s copy and paste so when the user is going to be logged in we want
home about profile and logout but when the users are not going to be
logged in then we want it to display the sign up and login option as well and all
right so my navigation bar should always have two options which are home and
about let’s go ahead and separate these two and okay
I’ll be adding the URLs here as well let’s go ahead and add it they should go
out okay we don’t have an about page yet but I’m going to create it
after this all right and then I’m going to check if the user is authenticated if
it is then we’ll have options such as profile and log out my navigation bar so
let’s go ahead and add this condition user is authenticated then should
display these two options yes so profile and log out and I’ll add
the URL over here I have to create user profiles after this yes so profile is
linked then I’ll have the URL to my flower page as well so URL and logout
all right now if the user is not authenticated then the navigation bar
should display options such as signup and logging so let’s go ahead and add
this condition else just play this already and I’ll add the URL over here
and it should go to user applications alright just inside user register right
so let’s go ahead and add the login page URL and login yes
let’s save it so let’s go ahead and create one new application called
about so that we can display our about view over there and let’s clear the
screen let’s start a new app and it started I start at what’s called it
about the enter let’s go back to our project and as you can see we have on
you application over here so I’ll just head on to my project directory over
here and I will just copy the URL files and paste it over there application so
let’s get back to the project yes as you can see my URL dot pi5 is copy over here
so now let’s go ahead and add our new application in the conjugations let’s go
ahead and open setting start by and i’ll add this new application in my installed
apps list and this is about alright let’s go ahead and create view and the
template that we are gonna render over there let’s go ahead and create the view
so over here let’s define a view function let’s call it about so it’s
gonna take a repost and it’s gonna render one external file which is be
called as about HTML and we are not gonna pass any context over here so
let’s go ahead and create the template and I’ll open template and create a new
HTML file and I’ll call it about let’s remove all of this and extend my base
file over here let’s start my blog content and it’s
here alright let’s go ahead and call the
navigation bar as well spill truth the navigation bar include my navigation bar
is in my lab on the scope our html5 so I just want to display a simple sentence
over here I’ll just add a paragraph tag and let’s say this is about us alright
so in my about page this simple sentence better show up let’s go ahead and create
a URL path in my main application which is a project and I will add one URL path
over here for my about page and just go like this
about and it will include my URLs filed inside my about a position so so
basically when somebody is gonna land in this URL path it’s going to point to my
QR else folder inside my about page so let’s go ahead and see what we have
inside that URLs page so we will change this view our view about view is called
about and let’s name it about on it and we can remove this from you and let’s
save it let’s go ahead and open our browser and just check it about pages
walking out all right let’s run our server once again
yes we don’t have any profile yet so it’s showing error let’s just create the
profile and then we’ll check if the pages are working or not so let’s go
ahead and create user profile so now we want our user profile to display one
form which will allow users to post their thoughts into the database so
basically in order to do that I have to create one model form and in order to
create the model form I need to start with creating a model over here so I
will just add a model with some fields inside my users application so let’s go
ahead and create our model class class that’s called post and this should be
your subclass of models dot model yes so let’s say I want my model to contain
some fields such as character field or text field so I’ll just copy and paste
over here and I’ll explain what’s going on my model contains user name and which
is a character field the maximum length of my character field is 200 this cannot
be null so next I have the post title and this will contain this is basically
the character field as well so the maximum limit is 200 and this cannot be
null as well next I have post content so this is going to be a text field and
here we don’t need to mention any character limits so since my post can
have a big amount of characters big number of characters right comparing to
user name and post title so we are using text field rather than using character
field over here so next we have date published so this
is going to be a date field and again last I have user profile link where user
can add their you know user profile or social media profile over here so this
is going to be a URL field so these things can be null but the main content
cannot be null such as username post title or post content so let’s go ahead
and save it all right now let’s head on to forms and let’s create one model form
over here so first of all let’s go ahead and import our newly created model which
is post and I will create a model form over here class let’s call it post form
and this is going to be a subclass of forms dot model form all right now I
want this parent class to have data from a meta class so let’s go ahead and
create my meta class and I will add the model as post that we have just created
and next the fields will have a list of fields such as user name and you know
post title and then we have post content right for something what else is there
and date published and user profile link all right then we have date published
and last one is user to a file alright let’s go ahead and save it alright now
let’s head on to views and let’s create a view over here and oh we create a view
from the model form that we have just created
we import it from both forms import most pop right yes so let’s go
ahead and create our view function let’s define it as profile and this is going
to be taking a request of course and then I will I can just copy and paste of
this part from here and yes this is basically same but I will replace it
with post form this is going to take yes post request and if the form is valid
it’s going to save it but we are not going to redirect it to anywhere so we
want the user profile to display a new model form where user can post another
thought right after one thought has been posted
so in order to do that I have to add form post form here and so we will return and render and then we
will head on to profile template extrema then we will render context which is
gonna be hard for me alright let’s go ahead and save this file at a space over
here yep let’s save it so user can only
access a user profile once logged in so in order to add that extra functionality
so we will be using something known as decorator so in order to do that I will
import from Django dot country dot thought and then decorator so I will
import the login required decorator and I’m gonna use it above this new function
and so this is gonna add one extra functionality to the function add it
right below it so basically let’s save it and let’s go back to our alright wait
we need to create a profile template Ezard
let’s go ahead and quickly create profile template so let’s remove
everything from here let’s extend our base file so let’s
start the block content and let’s end the block content so that we don’t
forget it afterwards so and block then and inside this I will include my
navigation bar include navigation bar dot HTML all right now for the form that
we are going to display over here is gonna look like this form which a
mistake because most method then let’s close the tag and since we are using
post method we have to use CSRF token then I’m gonna render my form over there
yes we will include the we will load the crispy forms tag as well so that our
form looks nice so let’s go ahead and do that let’s load crispy forms taxed
alright let’s add a filter over here which is going to say just crispy
alright let’s move ahead and we have to add one input here so this is gonna be
of type submit and it’s gonna say it’s gonna save a value and I will just save
this alright in order to avoid the overlapping of navigation bar and my
rest of the form so I just include this inside deep and
this is gonna be over here and I’ll add some styles which I’ve already added in
my rest of the palms so that just copy from one of them yes let’s copy and
paste over there let’s save it and now we can learn our server again let’s
clear the screen let’s run I think all right before we
run it since we have added we have modified the models that PI file we have
to perform migration so let’s go ahead and or migration first by to manage dot
v we make migrations hit enter so yes you can see one migration file has been
created for us so let’s go ahead and migrate it 9.5 and migrate let’s hit
enter and yes everything looks fine let’s clear the screen and we run our
server to run the server enter alright everything looks fine let’s go into our
browser and onto our first page oh it’s throwing one error
alright so it’s saying that we have not added n Tiff’s in our template file so
let’s go ahead to navigation bar yes we forgot to add the end if that over here
let’s end it and let’s save it now and let’s go back to our command prompt
let’s run this over again it says that it looks fine let’s hit enter another
trouble alright Reavers for profile not found so this is basically because yes
we have not added the profile URL yet so let’s go ahead and edit
let’s call it profile and I will remove this from here and I’m just read import
the user views and I have users hold deals I’ll import it as the card user
you just feel alright kill this view and I’ll add this inside my profile views
whose views dot profile is going to be my view alright let’s go ahead and name
it let’s call it profile so that our URL
can detected profile alright let’s save it and now let’s
clear the screen and run server again let’s go back to our browser let’s hit
enter yes everything is working fine let’s go ahead and log out yes my novel
option is not working let’s see what happened here let’s go back to our
project and let’s click on logout HTML so alright let’s just go ahead and
remove this part I don’t think we need that let’s go ahead and save it and
let’s go back to our command prompt let’s clear the screen let’s run this
over again it seems to be working fine let’s refresh it alright so we are in
our home page and about page is also life and let’s just register it let’s
call it dummy dummy 1 let’s say tell me one rate example.com alright let’s add a
password all right I messed up here let’s go
ahead and do it again alright so I’m in I can log in with dummy one and let’s go
ahead and try login alright so we are logged in as you can see my navigation
bar is showing a profile and logout option over here so let’s just try and
click on profile and yes I have a form where I can post my thoughts from so and
I can click on logout okay so this is showing if you are logged in but it
should have been your logged out let’s just fix it and you are logged out all
right let’s refresh it huh okay so let’s try and log in again and we’ll
just try to post one one of our thoughts yeah that’s and let’s click on profile
let’s say you tell me one is going to post something post name is something
let’s say and date published cuz 2019 let’s say 18 alright let’s go ahead and
add a profile link let’s say HTTP dot-com
alright let’s save it and as you can see once we save it our newly created or or
afresh of model form is displayed over here so that we can post our new thought
so let’s go ahead and log out so basically we are going to render the
post that we have just posted from our database to our
– bored or our home so we are going to display it over here and let’s try and
do that so let’s go ahead and head on to our home application and we need to
create one view over there so basically we are going to be using a class based
view so which is a generate built in view provided by Django so we’re going
to import it Django dot view dot generic and import
the list view alright let’s go ahead and create a context over here so that we
can put the context one and I’ll add it inside a dictionary let’s add a real
value say post and I’m gonna be displaying all the objects from the post
all right let’s go ahead and render this context and I’m just gonna say text one
alright so this is gonna be posting all right I need to import my post over here
as well since I’m using my post model here so basically I will just import
then from there I will import models and from there I’ll import the post alright
so now I will just go ahead and create the class post list deal and I will
inherit it from list view and let’s add some data here that’s called model and
this will be post and the template name is going to be my home page dot HTML
homepage dot HTML all right that’s extra space over there and now I
need to add context object name which is going to be the post that I was adding
the key name here yeah this is gonna be my context object name and then I am
gonna be displaying all the objects according to the date published all
right so let’s just say the date of leashed all right so let’s just save it
and let’s go back to our home template I will open it now I will add the section
for displaying my post over here so let’s go ahead and copy this part and
I’ll explain what’s going on over here so yep so once we land on our home page
it’s gonna say hello everyone and here are your recent post after this so
basically here we are looking over the object which is called post and we are
going to be displaying one post at a time inside this division so say I want
to display the post title from my post object so I’ll be using this tag which
is gonna be called as post not post title so similarly for posting sorry for
displaying the date published we’ll be using this tag and for post content I
have used one paragraph and styled it a bit you can just copy it and our can
start it on your own so let’s just go ahead and see our yes if we want to
display that username as well so we can similarly use this tag so let’s say we
want the users profile linked over there as well so we can use this tag inside a
a tag so basically this is the contribution that we have to do inside
our home dot HTML page so let’s go ahead and refresh our command prompt so let’s
just run the server once again enter let’s go ahead and refresh it all right
we are getting one error over here all right so let’s go back to our project
and let’s see inside home views dot pi okay this should be objects not object
so let’s save it and let’s go back to command prompt and let’s clear the
screen and let’s run the server once again I still managed by and all right
so it looks fine let’s go back to browser enter ok so inside template we
missed one thing we forgot to end the follow so let’s go ahead and yes we
forgot the end to follow let’s end it over here and all right let’s save it
let’s refresh it yes it looks fine and the post that we posted from dummy one
is being posted over here so all right let’s go back to our project and so now
let’s post another one and let’s do the same account
alright let’s go ahead and head on to profiles
let’s say done each one is again posted something then let’s say date published let’s say
2019 and let’s say 19 all right let’s just add one thing let’s just add simple
URL and let’s go to our home yes as you can see our new post is being displayed
over here all right then everything looks fine here let’s go ahead and
create the API and head on to our project yes let’s go ahead and create a
new application for API let’s clear the prompt and let’s create a new
application title manager dot fine start app and let’s name it idiot
enter let’s go back to project as you can see if you have been created here
and let’s copy and paste the URL over there
alright let’s copy the URL start by and paste it over there alright let’s go
back to our project so before we move forward we have to install Django rest
framework so in order to do that we have to head on to our command prompt let’s
go ahead and do that pip install Django rest framework alright enter so yeah I
have a Ganga rest framework already installed if you don’t have it then here
is the command that you need to use so let’s go ahead and go back to our
project first of all we’ll be creating one new python file over there and let’s
name it serializer it entered so what are we doing here is
I will import the post model from my users
and also I will import rest framework and the serializers so basically I’ll be
using models realizer to serialize the data so basically what’s happening here
is model serializer will serialize the data of post model into JSON format so
basically I’m trying to create one serializer here with the name post
serialized and I will be inheriting it from modern civilization so and the data
that I will be passing here is going to be provided by meta class the model is
going to be host and the fields that I want here are let’s name this field here
username then I have post facto then I have date published yes and also I have
user profile link did I miss something and I miss the post content that’s over
here all right let’s go ahead and save it so basically we will be civilizing
these data into position format so let’s save it now I will head on to Bill’s dot
PI and I will create a view set for our API so here I will be importing the post
model and let’s do that first of all I will give users got models input post
fed post and then I will also include the serializer that we just created
sterilizers and import all right and this is going to be dot serialized note
in the say Reisert I will import the city letter that we’ve just created post
selected alright and last but not least I have to import the model view set that
I’ll be using over here and this is inside rest framework dot view set
import model he said alright let’s go ahead and create our new set and let’s
create it post do you set and I will be inheriting it from model deep set now
let’s go ahead and type the query set over here and I will be using the data
from post model objects all objects from and I want it ordered by Heidi so
ordered by sorry this is going to be ordered by and ID so let’s say I want
first time in value so this is going to be shipped over here so then I will be
mentioning which serializer class that yangon it needs to use so the shaitaan
the class that we’ve just created over here so let’s mention over there and
let’s save it alright now let’s go ahead and set the URLs so basically in
API is we don’t use the conventional URL format so we are going to be using
routers instead so let’s go ahead and let’s remove all of this and let’s go
ahead import lest framework and from there we’ll imported outers and we’ll be
using simple router over here so from views we will be use we will be
importing the post view set that we just created so so we will create and
distance from the simple router router and then we will register the router
router but to register and post and the post you set so this is gonna be our
additional URL path that API is gonna create for us and last thing we need to
do is URL pattern setting URL patterns gonna be your router dot URLs so that’s
it let’s save this and before moving ahead we have to register this API
inside our main app teacher let’s go ahead and do that so we’ll just register
it now let’s go ahead and run our server just to check if things are working fine
managed to PI server yes everything is working fine
all right now in order to check the request method such as post or get we
will be heading to one software call man let’s open it yes we are inside
there’s doctor and let’s create one first let’s name it just one and we want
it inside okay I have already one project folder and I will save this
project and I will add the URL over here and I’ll copy and paste it over there
okay but we didn’t register the API URL in our main application so let’s go back
and register the application URL here yes we forgot to add it let’s add the
URL for our API and this is going to include API dot Evo’s
all right let’s save it we have a error here let’s see what okay we add a comma
here let’s save this project and yes everything is working fine let’s go back
to postman and let’s send a request all right we will be sending request let’s
call it API and post since we have mentioned the URLs here as post and we
will be sending the request given this so let’s go ahead and stand get requests
and let’s see yes we have two data records here and these records are
displayed over here all right let’s go ahead and send up post request
and in order to do that I will select post here and I will go to body and I
will select form data and here I will add my field my field names are username
host title post content date published and user
profile link alright let’s add some values here let’s add the username to be
entry button let’s say the word blog and that’s say post content and function
each end date operation is 2019 then a let’s add the URL part to tell apart
alright let’s go ahead and send this post request
alright this is done in order to check if this post request has been sent
successfully or not let’s go ahead and create another get request and as you
can see our new post has been created successfully in this session we’ll be discussing some
of the most frequently asked and important questions in any django
interview now i’m sure if you go through with all of these questions you will be
able to crack your next django interview so let us begin
so the first question is what is django now django is a server-side web
application framework written in python so any web applications usually consists
of two parts the client side and the server side the client side is any
application which can request data from a server this could be a web application
being run in a web browser it could be an Android application and iOS
application or any other kind of application which can request data from
the cell the server on the other hand is just a server which is running an
application which is written in a server-side language such as PHP Python
Ruby and so on so the job of the server-side application is to respond to
these requests so the server-side application will take the requests get
the appropriate data from the database if required construct a response and
then send it back in an appropriate format to the client
now django is written in python now python is one of the most easy to use
and popular languages out there so because of this django is quite easy to
use and which is why it is one of the most popular web application frameworks
being used now let us move on to the next question which architectural
pattern does there go follow so django follows we model you template
architectural pattern which is quite similar to the model-view-controller
architecture but for those of you who are unaware of this an architectural
pattern is just a way of constructing your code or managing your code so by
separating your code into smaller pieces it becomes easier to create bigger
projects and for that we use an architectural pattern so an
architectural pattern like model-view-controller
consists of three parts a modern a view and a controller so any code that deals
with data and the business logic so a code that the
with data manipulation data validation user authentication anything that deals
with the data is considered to be part of the model similarly views are the UI
elements the buttons the forms the images which the user sees and interacts
with and the controller is the glue that sits between the model and the view so
whenever the user interacts with the view the controller receives the request
fetches the data from the model if necessary and creates a new view or
updates the view with the data that it receives from the model now the MVC
architecture that Django follows is quite similar to the MVC architecture
django calls models models views are called templates in general and a
controller in Django is called a view so that’s just a difference in the
nomenclature of these application frameworks otherwise the functionality
is exactly the same so let us move on to the next question what is the difference
between a project and an app in general so in Django a project if the entire
application and an app is a module inside the project that deals with one
specific requirements so for instance let’s say you were creating an
e-commerce site as an entire project in that ecommerce site there are several
features that you need to implement such as the retailer’s site in which the
retailers could login and the products that they wish to sell and then log
similarly the buyer site in which the buyers would log in add the product that
we wish to buy in the cart and then buy those products and then log out so
instead of implementing all of these functionalities in a single codebase in
a single app what we can do is separate all of these functionalities into
smaller components or modules called apps that way several teams can work on
single applications without worrying about the other application so for
instance let’s say that you have a team working on the retail site map now any
change made to this codebase will not affect the code written for the buyer
side and vice-versa so this is why we use apps and projects in
so put it simply a project is your entire application and an app is a
single functionality or a module in your application let us move on to the next
question what is Django admin interface so Django comes with a fully
customizable inbuilt admin interface which lets us see and make changes to
the data in the database of a registered application and it model so if you were
to use any other application framework such as laravel Ruby on Rails spring etc
you would have to create your own admin interface or you can use a package to
install and configure the admin but in Django the admin interface comes in
built so for instance if you have an application in which there is a data
base and you wish to interact with it more visually you can use Django’s admin
interface so for instance I have created a sample application which is a simple
blog post in which you can post a single blog article and you can edit it and
delete it it’s quite basic so for the admin to be able to recognize your
database table and use it you need to first go to your applications admin dot
site dot register or the admin or py file and add the admin dot site third
register and the module names incorrectly that way in your blog
application you have registered your post model this is this one now what we
can do is we can go to the application as you can see there are five posts so
far now if I wish to add another post I can create a new post like this or if I
wish to use the Django admin interface let’s say I haven’t created the form yet
so what I can do is I can go to the admin login click on posts click on add
posts and then add the title at the body and then I can just save it as you can
see the post was successfully created now if I go to the post I can edit it
right here or what I can do at the application and you can see my
sample blog post was created successfully so instead of having to
implement this functionality from scratch triangle gives this
functionality to us for free and one of the most important features of this is
you can track the changes that you have made so far so for instance you can see
that I have created an object another object another object we these are just
articles that I created and then I deleted one of the Articles that I
written is an article that way you can keep track of the changes that you have
made and you can edit the data that’s been inserted so let us move on to the
next question the next question asks us to explain the request response cycle in
general so in Django a request response cycle basically means how the request
enters the Django application how it’s converted into the response and the
response is sent back to the user so the first thing that happens is the user
enters the URL of our application the URL is then received by our server which
will match the URL with a list of existing URL patterns if a match is not
found then a request a response of 404 is sent back to the user similarly if a
match is found then we registered view that is registered with the URL pattern
that matched and then execute the code inside it generate a response and send
back so for instance we have a sample application in this this is the URL dot
py file or the base URL file of our project any request that comes to our
application will first hit this file so for instance let’s say that we go to our
base URL a base URL is basically your site’s domain if you have deployed your
site then it’s the domain name that your site users like www-what simple blog
comma or anything else right now we are testing things out on a local
machine so it’s local host : which is the port number so we make a
request this request goes to the urls file now simply because it’s just a base
URL it will look at the empty string if for instance the URL was slash admin or
slash api or any other string appended to the youth base URL the application
will look at other urls such as admin API and if the appended URI
matches to the registered URL then that view would be called right now sit is
the base name we call log dot URL which is the blog applications you are it’s
fine just inside it since again it’s the base URL this empty string will be
evaluated and inside the views file the post ListView is created this is the
post ListView in this what we do is we we find the model that template the
context object name and the ordering and according to the code the response is
generated this is an HTML response and it’s sent back to the user and we have
all our posts formatted nicely as the response let us move on to the next
question what is a model in general so a model is simply a Python class that is
derived from Django or DP dirt models dot model you can think of a model as a
representation of our table the fields in the class of a model is our are the
fields that will be used in the database so for instance our blog model which is
this one the post model this inherits from Django dot DB dot model but model
and inside it we have described a title and a body these are used to create and
map database entities to our table so let’s move on to the next question ok so
let us move on to the next question the next question is what are migrations
in general so a migration is a simple Python file that contains the changes
that we make to our models these files are then evaluated by Django and are
converted into a database schema so instead of manually running sequel
queries and making changes to a database schema like adding or deleting a row
adding a column deleting a column adding a foreign key so on and so forth what we
can do is just make changes to our models in a Python code then create or
generate a migration based on that model and then that migration is then run to
make changes to our database so for instance in our post model we have the
title which is a character field and a body which is a text field so what we do
is we create the model we save it and then we generate a migration which is
same in the migrations folder in the same application and as you can see the
name of the table is the same as the name of the model which is posed then
there’s an ID field which is added by default it is used as the primary key
similarly we have the title and the body this is then run by our Django
application to create or make changes to our database schema now this allows us
to make changes to our Python file and have these changes reflect back on our
database without us having to write a single line of sequel quit so let us
move on to the next question so what are views in general so as we have seen
models are used to deal with the data and views are used in Django as
controllers so views are used get data from the model and display them to the
template a view is basically a class or a function that receives the request and
that returns the response of view is usually associated with a Jol pattern
which is matched by the Django application and then the logic
encapsulated in that view is when the URL matches a view among other things
gets data from the models and passes the data to the templates for them to render
and then convert that to an HTTP response and sends it back to the user
so for instance as you can see in application we get a get request to the
blobstore to urls file then this is the view that is used to generate the
response and send it back to the user inside the view this is the model that
is being used to get data from and this is the template in which the data is
being passed so let’s open the template and see the template is basically just
looping over all the posts and creating an or generating the HTML content and
then send it back as an H response now this is all handled by the
view abuse job is simply to sit between the model and template and interact with
each other so let us move on to the next question so the next question is what is
the use of the include function in the URLs dot py file in general so as we
know that each Django project contains several applications now instead of
registering all the URLs for all the applications in a single file what we
can do is make each application contain its own URL star py file and then the
base URL dot py file will reference these application specific URLs file to
get the list of valid urls that these applications can respond to so this is
done by the include function so for instance as you can see in this you
registered URL what we do is if any request comes to our base URL it will be
handled by the blog applications you are a spy this string represents the blog
folder and the URLs file so as you can see this is the plot folder inside it we
have a URL stored py file and in this we have registered some of the URLs so if a
request comes to localhost colon 8080 you will be responsible for generating
the response so in this we give it the template the form class the success URL
and so on and so forth any other parameter that if needed can be defined
here so when we go to localhost / new this form is generated and sent back as
the response this is done by post create view which is routed to by our
application Shiva alpha so we use the include function to include each
applications specific URLs file into the main URL file so let’s move on to the
next question why is Django called a loosely coupled
framework so Django is called a loosely coupled framework because of its MVT
architecture as we’ve discussed before MVC is just a slide
variation on the MVC architecture it helps in separating the server code from
the client so Django’s models and views take care of the code that needs to be
run on the server like getting the code from the database and the templates and
mostly the HTML and CSS file that needs the data being passed from the model to
themselves so that they can render them so it’s called a loosely coupled
framework because these components of MVT architecture can work independent of
each other so for instance if I were to make some changes to our models dot py
file by making changes on how it gets the results from the database that will
have no effect on our templates similarly if we make changes to a
controller which is responsible for getting data from the model and passing
it down the template that will also have no effect on our template since these
parts are usually independent from each other which is why Django is called a
loosely coupled framework now let’s move on to the next question so the next
question is what is Django over power M stands for an object relational mapper
now this term sounds quite intimidating but it’s fairly easy to understand so
instead of interacting with the database by writing raw SQL queries and
converting the data that is returned from the query into Python objects like
we would have to do if we were building any kind of web application written in
Python we can use in orem which allows us to interact with the database using
objects of odd module class so we interact with our models and the ORM
converts these changes into SQL queries based on the database we are using like
SQLite ms SQL MySQL etcetera the over m will take care of the changes in the
database so for instance if you were to use MySQL in an application that’s
already being run on SQLite R over m will adjust the queries for us so let us
take a look at the example so here I have a simple application I will
create or Ana create a shell environment in which I will be using this
application so let me first in toward the modules that we’ll be using
now as you can see if I want to get all the records in the database what I can
do in simple Python terms I can just use the methods on the clear so these are
all the posts that are available in our database as you can see I did not have
to write a single SQL query all of this was managed by our / M
this can also be used to create new object save it to the database retrieve
single objects like if we use post not objects dot filtered get the response as
the room that has primary key one and we get a single response from the / M they
are used to get the data from the database and convert them into the
Python objects that we can use in our Django application so let’s move on to
the next question which is how do templates work in general now templates
are used to dynamically generated web content Django’s templating engine
handles templating that involves passing processing and then converting the
template into an appropriate HTTP response that is sent back to the client
these templates by default are written in Django templating language although
you can switch to another templating language like ginger – which is a
popular templating language in the Python world now these templates will
allow us to generate dynamic content based on the data you pass into the
template by the view so for example in this application in our view we get the
data from our post model then we send the data to the index template as you
can see that these are all the static content so for each one closing each one
another each one these are all static content and anything that’s wrapped in a
curly brace or two curly braces these are all pythons or chango’s templating
language constants so in this what we are doing is we are looping over all the
posts and dynamically creating new blog post which can be then rendered in our
template so we loop over all the posts display the current post title
and current post body bracelet if we have 10 posts then it will be looped
over ten times and ten HTML div items with the post trapper class and title
and body will be checked then this is sent back to the user and what the user
get is and what the user gets is this page as you can see all of the posts are
in the exact format that the template described and everything is rendered as
the template was created so let’s move on to the next question so how do we
register a model with the Django admin so to register a model with the Django
admin we need to make some changes to our applications admin dot py file this
file is automatically generated by Django
so we don’t have to create it ourselves although we do have to make some changes
in it to register our model with the admin which will give admin the
permissions or it’ll make the admin interface aware of the model that we
wish for it to use so for example if suppose we had a post application will
open the polls not poles applications admin dot py file import the model let’s
say the Model T name is question and we wish to register it so we’ll write the
code admin dot sight the register and in the brackets will write question because
register is a method and then this will end up registering our questions model
with our admin site then we can login to our admin site and then use the admin
interface to make changes and get the data from this model so for example as
you can see we have our application inside it when we look into our
applications admin but py file which is this one as you can see I have
registered the post model which is what enabled us to log in to our admin site
and then use the post database object now for instance if I were to create
another model the admin will not automatically pick it up then I will
have to go back into that applications admin tour py file type in admin dot
side not register and import the model and then registers
a model like this then and only then will our application use the model with
the a D so let’s move on to the next question so let us move on to the next
question how do we generate a superuser injector
now a superuser is basically the admin so the credentials for Super User are
generated by using a Python command in which we use the manage toad py script
in the command prompt so we type Python manage dot P wine create super user or
lowercase with no spaces in between and without the quotes then the command will
ask us for some information like the username the email and finally it will
ask us to enter the information about the passwords two times for confirmation
this will create a super for our Django project so for instance if I were to
create another super user for this application what I can do is type dy so
in our command from what we need to do is we need to type the command py you
make sure that if you are using Ubuntu or Macintosh you need to type in the
complete come on with pies since I’m using windows I can type py manager dot
py create super user all lowercase no caps no spaces I click enter and it’s
asked me for the user name let’s call it email address I write tests and example
comm it doesn’t really have to be a real email address because we are using it
for testing purposes for password I am simply going to write a small password
similarly now in a production environment it is usually considered a
bad practice to use small passwords however for our use case it’s quite
already so the Django is giving us all of these errors or rather the warnings
which tell us that we need a more stronger password a more stronger
username and stuff like that but we can use it here because it’s just for
testing purposes so we created the super user successfully now if we run the
server again and then go back to our site and now I use
our flesh traditions to login as you can see I have been able to log in using the
user that I created right in front of you so that’s how you create a super
user in Python moving on to the next question so the next question is
mentioned some disadvantages of changing Django has the following disadvantages
chango’s modules are quite bulky now this is in disadvantage if you are
building a really small application because you’ll have to manage a lot more
code then you would typically have to if you were building it from scratch that
is without using the jungle for instance if you were building a really small
to-do application which will just store the the things that you need to do in an
entire day in the database and then deleting those things once you have done
them so doing such a small application for building such a small application
using Django might be a bit of an overkill the next disadvantage is it is
completely based on Django’s bottom so if you were not quite fond of Jen goes
over them if you were uncomfortable with it and wanted to use another over a
meter in Python Django doesn’t allow for you to do that
because Django’s entire ecosystem is quite tightly bound with the Django so
if you were to replace it with another or M it will not work so unless you are
comfortable with Django over m using Django could be a bit of a difficulty
the next disadvantage is components are deployed together
so in Django all the components are deployed together even if you were to
make a small change to one of the smaller components you will still have
to redeploy the entire application together with all the components which
could be a little difficult if your application is quite big now the last
disadvantages we must know the full system to work with it so if you were
unaware of how the modelview template architecture works how does the request
get into the application how is it converted into the response what steps
are taken how does it match the URL so on and so forth it would be difficult
for you to create an application in general because you will have difficulty
figuring out where to add what code in order for your
application to run so let’s move on to the next question so the next question
is what is sessions framework in general so in Django sessions framework is used
to store arbitrary information about the user on the server in the database
sessions are mostly used to recur through differentiate logged in user
from users that are not logged in as in guest users this is mainly done because
HTTP protocol which is used by all the web applications out there is a
stateless protocol which basically means that requests do not store information
about subsequent requests so for example let’s say that I log in to an
application by sending it my username and password so the server will receive
my request for logging in with my username and password it will verify my
credentials and log me in and send back a response of HTTP 200 which means
everything went fine now whatever requests I send after that HTTP has no
way of storing information about me being logged in or locked out so any
requests that I sent after that using HTTP will not be taken as an authorized
request or a request from an authorized user so to overcome this problem we use
sessions sessions basically work by storing a special session ID into the
user’s browser using a cookie this session ID is just an arbitrarily long
string with alphanumeric characters whenever the user is logged in and he
sends any request after that this session ID is included in the request
then the application will look into the database look if the session ID is there
and it’s valid as in the user has has not been logged out yet and then that
user request is treated as a request from a logged in user the session ID
also helps the application to figure out the user’s name user ID and any other
information about the user that may be relevant which can be used for
authorizing actions on the application so let us move on to the next question
so the next question is what is a cookie in general a cookie is just a small
piece of information that is stored in the clients browser it is used to store
users data in a file permanently or for a specified amount of time a cookie has
an expiration date and time and it gets removed automatically when it gets
expired Chango provides inbuilt methods for
setting and fetching the cookies as we saw on the previous question
cookies are quite important for the sessions framework to work cookies are
used to store the user’s session ID which is then used to authenticate and
authorize the user as a logged in user now cookies can be used to store any
piece of information on the clients browser however it should be noted that
storing non sensitive piece of information in cookies is quite all
right however storing information that is
sensitive like users password users credit card information or any other of
information that we wish to keep private should not be stored on the cookies as
cookies are stored on the client side any user who has access to users browser
can access the cookie and get the information from the cookie moving on to
the next question what is a middleware in Django so a middleware is just a
layer in Django’s request/response processing pipeline as a request enters
Django arm Django application and the response is sent back to the user there
are certain steps that Django takes these steps are arranged in a pipeline
and each step in the pipeline is called a middleware each middleware is
responsible for performing a specific function on the request and/or the
response before so functions such as caching and gzipping are part of the
request response pipeline for instance if we were to authenticate the user we
can use Django’s authentication middleware so for example let me show
you so in our applications settings dot py
file as we can see we have a list of installed apps and right below that we
have a list of metal bits these middlewares
are the layers in our request/response pipeline
so what happens is a request enters our application and it is then processed by
each of these layers and if all of these layers passed the request and passed the
response if the middleware is supposed to access the response then the response
is sent back to the user otherwise if there is an error that that error is
sent back to the user so for instance this middleware is for authenticating
the user so any requests that can only be sent by an authenticated user like
editing a blog post this is taken care of by this middleware and there are
other method middleweights regarding security and performance and so on and
so forth so let’s move on to the next question so
the next question is what is a query set in general so a query set in Django is
basically a collection of objects from our database query sets are used by the
Django ORM remember ORM stands for object relational mapper when we use our
models to get a single record or a group of records from the data which they are
returned as queries it so Django’s ORM after fetching the
records from the database fetches them in a type which is known as queries like
visual so as you can see I am in our application and I’ll run the shell to
show you how well such are being used so let us first try to import our model so
from modern post now if we try to fetch all the records from the database using
post dot object dot on you can see that we are getting back a result set in the
form of equation similarly if you were to fetch a single code from the database
such as post dot objects dot filter get record with the primary key of one we
get a single response from the database which is wrapped in a query set these
query sets are used by Django’s ORM to perform filtering and querying to the
data that is returned back from the database so let’s move on to the next
question so the next question is what is Django
rest framework so the Django rest framework is just a django app which is
also a framework that lets us create a restful api rapidly Django rest
framework is especially useful if we have an existing Django web application
and we wish to quickly generate an API for it for those of you who are not
aware of this a REST API is basically a web api that allows any other sort of
client such as an Android application an iOS application or any other application
to access the information on our server so for instance let’s say we were to
create an Android application that application would need to get data from
our server and it does that by sending an HTTP request to our REST API now
Django rest framework makes creating a REST API out of an existing Django
application quite easy which is why we use the Django rest frame let’s move on
to the next question what is a context in general so when we are rendering a
template certain parameters or data need to be passed to the template by the view
that data is passed in a dictionary dictionary as in the Python dictionary
type and that dictionary variable is called pick context a context is
basically just a dictionary in which the keys represent the variable names and
the value to present the values of those variables this dictionary is used by the
templates to output dynamic content so for example so in our application when
we look into our views file as you can see that we have this context object
name similarly the context object name is here as well
so let’s look into the blog index.html go to the templates inside block we have
index dot HTML we open it and as you can see that this variable name matches
exactly our context object name so as you can see posts and posts so what
happens is after fetching the all the data from the database the view passes
all the records or the in a dictionary named posts when
rendering our template then in the template we loop over all the posts and
displays the data so a context is basically just a dictionary with key
values where the key represent the variable name and the values represent
our values for those variables this is then used by our templates to generate
the dynamic content that we need so let’s move on to the next question so
the next question is list some of the caching strategies that Django suppose
so Django supports four types of caching
strategies file system caching memcached in-memory caching and database caching
so file system caching is basically caching anything that we will need to
cache on the file system by creating a file such as a log or a JSON file or any
file now this is good for small applications but for larger applications
accessing data from file that is opening the file managing the file and closing
the file it takes quite a bit of performance hit so for larger
applications we usually tend to use a dedicated service like memcached man
cached is its own service and particular job is to keep a cache maintaining a key
value pair removing the cache if it’s invalidated so on and so forth so django
supports memcache and filesystem caching in memory caching is quite good for
because it will not have the same latency effect that a file system cache
in will have data stored in memory is quite fast to access than the data
stored on a file system however we have limited resources when considering the
memory capacity so using in-memory caching for smaller
applications that require fast access could be a good idea however for larger
applications memcache is still preferred now the last one is database caching now
this is good if you wish to cache an object that you need to use quite self
after quite some time in future so because getting data from the database
is also an expensive operation you need to open the connection from the database
then ask for the data by writing a query and getting back the response and then
parsing the response so caching in the database is only good if you are
catching something that is accessed infrequently over long periods of time
so these are all the for caching strategies that is supported by channel
now let’s move on to the next question so the next question is what is a meta
class in Jango so a meta class in Django is an inner class that provides metadata
about the outer class in Django this metadata can be thought of as simply
configuration parameters the ten Xango how we want it to behave so for instance
it allows us to define such things as permissions associated database table
name singular and plural versions of that name and so on and so forth
so for example in our application we have a model form which is called post
form this form is taken care of by Django itself so all we need to do is
provide some configuration parameters or metadata such as which model do we want
it to use this model is then used by the post form to save data or fetch data
from the database also we are giving it the name of valid fields so if suppose
we have a database table that has fields such as the ID the title the name date
published and date modified and all we wish to do is provide users with the
ability to enter the title and the name of their post and submit it to our
application well then this form will do it for us all we need to do is provide
an array of attributes in our meta class which are these similarly if we want the
user to be able to submit the publishing date we can modify this array by adding
another item in it which is the name of the database field just it published
however in this case just read the title and the body is fun so a meta class is
simply used to configure our Django applications behavior by providing meta
information about the application so let’s move on to the next question so
the next question is when should we generate and apply migrations in a
Django project and why so we should do that whenever we create
or make changes to the models of one of our tabs in our project this is because
a migration is used to make changes to the database schema and is generated
based on our models so if I were to make change to our models by adding a field
removing the field changing a field this change should be then reflected into our
database schema which is only done by running the migrations after creating so
whenever we make changes to our models in any of our apps we need to then
generate a migration and then apply that migration that way our database schema
will be in sync with our models so let’s go on to the next question
what is serialization in general well simply put serialization is the process
of converting Django models in to other formats such as XML JSON and so on so as
we saw previously whenever we get data back from the database fusing chango’s
ORM we get it in the form of a query set now that query set is then used by our
Django application to display data on our application however if we wish to
convert that data into another format such as an XML format or a JSON format
which we can use easily to send over the Internet this is done by serialization
so let’s look at how civilization works so as you can see we have logged into
our Django shell and we’ll try to serialize one of the query sets that we
get from the database so first let’s import our model so from log your model
import oops now let’s import the serializer so from shanku import
serializers now that our put it when we get a single row from the database we
get it in the form of equation so for instance post dot objects dot filter
here will get a single row with the primary key one so this is what the data
looks like it’s a query set with one post object however this data cannot
sent to another client using in HTTP response so to convert it into another
format like JSON or XML or any other format that can be passed and sent as an
HTTP response we use a serialization so for example let’s convert this data into
the JSON format so see license dot realize this takes two parameters the
first is the format which is Jason and second is equal shit in which case it’s
our data and as you can see it’s formatted nicely in the JSON format it
contains the name of the model the primary key which is one the fields that
are in our database such as title and body and that value now Jason is not the
only format that we can see religion you can serialize it in XML as well however
JSON is usually preferred over XML because it’s not less verbose and easy
to read and parse so if I were to serialize it in the XML format we will we get it the name of C realizes I’ve
spelled that incorrectly he realises passing the moment and as you can see we
get an XML format and as you can also see that the XML formatted string is
quite a lot bigger than the JSON format history so you can serialize the data in
whichever format you prefer and using the JSON serializer we can serialize it
in the JSON format or we can stabilize it in XML format so this is why we use
serializers to convert complex objects that we get from the database using
chango’s ORM into readable and possible formats like JSON or XML
moving on to the next question so the next question is what are generic views
when building a web application there are certain kinds of views that we need
to build again and again such as a view that displays all the records in the
database displaying all the books inside a books table can be sort of as an
example these kinds of views perform the same functions and that leads to
repeated code to solve this issue django uses a class-based generic view / based
views can be used by inheriting from the desired class from Django dot views but
generic module and provide some information like the model we wanted to
use the context object name etc so for example in our sample application we
have a post list view it’s job is to get all the records from the database pass
it into the template that is blog slash index dot HTML and display all the
records that it fetched from the database so for doing that we would
typically need to instantiate the model get all the records then then pass it
into the template by building a context object so if we were to remove the
duplication in the code what we can do is just inherit from the generic view
list view as we have done here and we simply specified the parameters that we
needed to use so the model that this view will be using is post to template
which it will be using to rendering the model would be blog slash in the
studies table the context name would be posed and ordering will be by ID in
ascending or that way it will a response and sends the response back to the
client let’s move on to the next question so the next question is which
companies use django django is quite a popular web application development
framework many big companies use Django for several parts of their application
for instance Instagram uses triangle so let’s discuss Mozilla bitbucket
Spotify NASA Eventbrite even poorer uses Django so as you can see that Django is
quite a scalable web application framework since it can handle traffic
for such large web sites so let’s move on to the next question so the next
question is which class do we inherit from in order to perform unit test we
inherit from the Django test case class in order to perform unit tests as it
contains all the methods we need to perform unit tests methods like assert
equal assert true so on and so forth let’s move on to the next question so
the next question is which class do we inherit from in order to perform
functional test we inherit from Django dot test don’t live server test case to
perform functional tests this allows us to launch in life Django server in the
background on setup and it shuts it down on teardown this allows us to use an
automated testing plans such as selenium in order to perform functional tests by
launching a browser instance and then simulating real users action like
clicking on a button submitting a form so on and so forth let’s move on to the
next question what is the Django to test but client class used for the client
class acts as a dummy browser allowing us to test our views and interact with
our Django powered application programmatically this is especially
useful for doing integration tests so for instance imagine that you have
created a URL pattern and have created the view associated with it now you need
to test that if that view is being loaded correctly and the browser is
responding to that URL pattern we can perform this kind of a test using the
test client plus from the jacket so the last
question we have is what is the Django dot test dot client class used for so
the client class acts as a dummy web browser that allows us to test our views
and interact with our Django powered application programmatically this is
especially helpful for doing integration testing for example let’s suppose that
you have created a URL pattern and associated of you with it now we wish to
check if that URL pattern is performing correctly to do this we can use the
Django – client class by giving it the URL pattern that we wish to test and
then check the response if it returns a response code of 200 which means
everything went successfully that means that our view was correctly registered
and URL is being correctly captured by our Django application these kinds of
tests are called integration tests because they use several parts of our
application and check if they are working correctly together quick info
guys if you’re looking for end-to-end training in Django do check out in
telepaths Django certification training course the link is in the description
box below so guys I hope this video is helpful if you have any queries don’t
forget to leave them in the comment section below thank you so much for
watching

10 Comments

Add a Comment

Your email address will not be published. Required fields are marked *