Python Django Tutorial | Learn Python Django In 3 Hours | Python Web Development | Edureka

Python Django Tutorial | Learn Python Django In 3 Hours | Python Web Development | Edureka


Hello everyone. My name is Aayushi and I welcome you all to
this Django Full Course Session. Now, I know many of you will be
excited just by its name. But before we actually get
our hands on it, let’s see What’s our agenda for today. So we’ll start of the session by having a quick
introduction to Python. And why do we need it? We’ll also have a look
at the various libraries that python offers and why Django is so important
We will also have a look at the Django features and architecture. Now Django offers an MVC
and MVT architecture, which basically means
model view controller and model view template. Then we move forward and learn the prerequisites to
learn Django and will also see how to install Django. Then comes
the most exciting part that is how to create a website
from the scratch. Now this will include
both your content as well as backing now, we also see how do you connect
databases with the project and will also
test the application? Moving ahead. We’ll see the
Django rest framework. And how do you create
a simple apis using Django moving head
will also see some dos and don’ts and some frequently
asked questions regarding Django and finally I wrap up
the session by discussing some of the important Django
interview questions and answers. So I hope you guys are clear
with the agenda. So let’s begin also, don’t forget to subscribe to any
vehicle YouTube channel and hit the Bell icon to stay updated. Thank you. We shall first begin with
a small introduction to python followed by the different
web development Frameworks that are available in Python. Then we shall take a look
at what exactly is Django and the features of it following that we should take a deep look
at the architecture of Django followed
by its installation and then I’ll be showing
you all how to create your own website from scratch. Finally. I’ll be covering
a few Django interview questions before winding up. Okay. So without any further delays, let’s get started python as we all know is a high-level
interpreted programming language created by Guido van Rossum in One it’s a great
general purpose language capable of creating anything from desktop software
to web applications and Frameworks talking
about web applications in particular python allows you to dynamically build
web applications grants East to build
multi-protocol networks provides quick processing lets
you build more functions with fewer lines of code. And the best of all, you don’t usually
mess up using python unlike other languages apart from that python also
provides a number of web development libraries
such as cherry pie Django web 2 pi pyramid turbo gas flask
Etc talking about Django. It’s a high-level python web
development framework that encourages rapid development
and clean pragmatic design. Django is an open
source web framework, which was named
after Django Reinhardt. It follows the principle
of don’t repeat yourself as the name says
the principal is all about keeping the code simple
and non-repeating Django is also a high level mvt architect. Stands on the model view template Django along
with python is used by many of the top companies such as
Google YouTube Dropbox Etc. Now the reason why all these companies
make use of python along with Django is because of
the remarkable features that Django provides. Now, the reason why
these companies make use of python along with Django is because of the key features of
Django Django is extremely fast and encourages rapid development
with a clean pragmatic design. Like I’ve already mentioned before it’s free
and open source, which helps developers
to complete their app as fast as possible apart from its speed Django also provides
a huge number of packages that help you develop
your websites faster and easier. It is also highly secure
and scalable last but not the least
Django is versatile, which allows you
to build any sort of web application you desire. So now let’s move on and take a look at
the Django architecture Django follows the MVC mvt
architecture MVC actually. Stands for model view
controller it is used for developing web applications where we break the code
into three different segments namely module View and the controller a model
is basically used for storing and maintaining your data. It is the back end where the database
is defined a view is all about the presentation and it is not at all aware
of the back end. So basically whatever
the user sees on a webpage is returned by the views
component the controller on the other hand is
of business logic which will interact
with the model and the view now talking about the MV t or the model view
template structure in the MVC architecture. There’s a predefined template
for the user interface. For example, say you want to
write several static HTML forms, like hello user 1 hello user do
and so on with template you’d be having only one file
that prints hello along with the variable name. Now this variable
will be substituted in that particular template
using some ginger logic. That’s the magic of template you don’t need
to Add the goat again and again in case of mvt Django itself takes
care of the controller. The controller is inbuilt into Django and you
don’t have to bother about it. So taking a deeper look
at what we were talking about before so basically
a template is a front end which will interact
with the views and the model will be used
as the backend the viewers basically acts as both the model and the templates
and Maps it to the URL after that Django plays
the role of controller and serves it to the user. So given this I’m sure
you have a brief idea about what is Django
and it’s architecture. So now let’s move on and see what are the prerequisites
required to learn Django. The first thing that you’ll have to know is
the oops Concepts this is because we’d be
creating many classes objects sub classes Etc. Also, you’ll have to know how to use the Python
programming language SQL queries html/css Etc in case you have
difficulties with any of these you can check out
Erica’s YouTube channel and the blog series which will Only help
you so moving on. I’ll be showing you all
how to install Django on your systems. Okay. So one thing I want
you all to note is that I’ll be using pycharm. You can use any ID
of your choice, but personally I feel pycharm is
the best for creating websites. So the first thing you’ll have
to do is download python on your PC’s to do that. You can go to the official
python web site. Go to downloads
and select any version that suitable for your PC’s
and select the installer that is suitable
with your operating system. Once you’ve downloaded python, you’ll have to go
to the jet planes website to download pycharm on your PC’s
so after downloading python, so after you’re done
with the setup go to the advanced system settings. Choose the option
of environment variables and over here in the system
variables choose the new Option and type python underscore
home now to enter the value of this variable. You’ll have to look for the part where you have installed python
on your PC’s to do that. You can simply type python
in the search bar. Right click on it
and open the file location after this simply copy
the path and enter it as the value of this variable
then click on OK. So after you’ve done
that select the path variable that’s present select
the path variable and towards the end of it. If you don’t see a semicolon add a semicolon and within
two percentage symbols, as you can see over here
write python underscore home once this is done click
on OK So now to download pycharm on your PC’s simply
Type download pycharm. And then you can download it from the official jetbrains
website select the version that’s suitable for your OS
and you can choose between the professional
and the community version where the professional version is paid and the community
version is free. So after you’re done
with the pycharm installation complete the setup
and open pycharm on your PC’s since I already have
it downloaded on my PC. I’ll just open pycharm. So once you’re done
with the setup, you will be seeing something similar to what you see on the
screen when you open up pycharm. I’ve already created
a project over here. However, if you want to create
a project click on file and select new project, it will ask you to enter
some name just give some name of your choice and hit
the create button. Okay. So after creating a project
open up the terminal and type whip install Django. So when you’re installing Django along with Django
SQL parse data base and the PYT said module also
gets installed SQL parse is actually an on validating
SQL parser for python. It provides support
for passing splitting and formatting SQL statements. Now, this is actually
compatible with versions of python from 2.7. The pyd said module brings the Olsen teaser
database into python, this Library allows accurate and cross-platform
time zone calculations using python 2.4 and higher
it also solves the issue of ambiguous times at the end
of daylight saving time. However, this Library
differs a little from the documented python API for tea set in
for implementations. So now coming
back towards Django. Once the installation
is completed. You will see a message saying
successfully installed Django and the version of Django. Now to cross verify if Django has been
successfully installed go to the command prompt. And type python –
em Django – – version. So as you can see when you type this command it
Returns the version of Django that’s been installed. So now going back to pycharm. I’ll be showing you all
how to create a Django project to create a new project. You’ll have to make use
of the Django admin to do that type Django – admin start project followed
by the name of the project. You can give any name
of your choice guys. I’ll just name it as website. I’ve made a spelling mistake. I’ve missed in from Django. So now my project
has been created and as a CD into my project so CD
and the name of the project, which is website. So now I’m in the project that I’ve created with the help
of the Django admin to see what the Django admin
has created for you within this project click
on the project directory. So as you all can see
on the screen the Django admin has created a number
of files for us. The outer website root directory
is just a container for your project
the name however does not matter to Django and you can name it as
anything you like after that the very important file
that Django creates for you is the manage
dot py file the managed of py file does the same
thing as Django admin but takes care
of a few things for you. It puts your projects package
on system path. It sets the Django settings
module environment variable so that it points to your
project settings dot py file. Next is the in its dot py file. This is an empty file
but tells python but this directory should be
considered as a python package. A package is a way of structuring pythons
module name Space by using dotted module names the in it dot py files
are required to make python treat directories
containing the file as packages. This prevents directories
with a common name unintentionally hiding
valid modules to occur later on the module search path in the simplest case in it’s
not py can just be an empty file but it can also execute
initialization code for the package followed
by that is the settings not py file this file actually
consists of the settings or the configurations
for this Django project. The Django settings dot py file
contains all the configurations for your Django installation. It’s basically just
a python module with module level variables because the settings file
is a python module. It does not allow
python syntax errors. It does not allow
for python syntax errors, and it can assign
settings dynamically using normal python syntax. It can also import
values from other. Files so now let’s take a look
at the URLs dot py file. Let me just clear these comments
the URLs dot py file consists of all the URL declarations
for the Django project. It is basically like a table of contents
of your Django part site. So basically when a user
requests for page from a Django path site
this file determines which python code to execute Django determines the root
URL conf module to use loads that python module and looks
for the variable URL patterns. So this should actually be
a part of the URLs instances Django then runs through
each URL pattern in order and stops at the first one that matches the request
to the URL. Once that is match Django
Imports and calls the given view which is a simple python
function that returns to the user the page
that he has requested. So now let me show you all
the wsgi file the wsgi done py file is an entry point for
the wsgi compatible web servers. So if your project wsgi actually stands for
web server Gateway interface, it is a specification
that describes how a web server communicates
with a web applications and how the web applications
can be chained together to process one request. So now I hope you have a clear
idea about what these files are. And what is the purpose of
creating them now to make sure that your project is running
simply type python manage that py run server. Sorry, I forgot to give
a space over here. Okay, so as you can see
when I hit this command, it has returned
a URL for me now. Let me show you all what happens
when I click on this URL. So congratulations guys, when you see this rocket,
which is ready to take off. It means you’ve installed
Django successfully without crashing your systems. So now I’ll get back to pycharm and show you all
how to create an app. Ok. So now let’s get back to pycharm
the development server. However will be running unless
and until you press control C in the terminal all the changes that you make
to your project will be seen on the development server just
by refreshing the page. Therefore, you don’t have
to type python manage that py command each time
at this point of time. I want you all to know
the difference between a website and a web application a web site
is actually a bigger domain that contains different web
applications a web application is a part of a website if you’re calling
a website your website is basically the main project which contains many other
web applications in it. So whenever you visit
any website, it consists of different applications that are joined together
to make one complete website. The application is basically
A sub part of that website which has a specific
task and app is basically a web application that does something a project on the other hand
can maintain multiple apps and an app can be
in multiple projects. Okay. So now to create an app, I’ll go back to my terminal hit
control C and then type python – 2py startapp followed
by the name of the app. You can give any name
of your choice. I’ll just name it
as technical courses. Sorry guys, I’ve given
a space by mistake. So now my app technical courses
has been created. Now. When I go back to our project
I can see over here that my app technical courses
has been created. When you open this you
can find the Django has created many other files again
for us the apps file which consists of a class for
all the installed apps models, which I’ve already explained
to you all before a test start py file and the views file. So now let me run
my server again. So one thing I want
to all to note is that Django already knows that each website will have
an admin section to access that you can simply type slash
and then hit admin. So when I type admit over here, you can see that it’s throwing
an error now this is because we have not
created any admin. I just came to this page
to show you guys that this page actually exists
and it’s already created and the settings for it have
already been created by Django. So now let’s get back
to our home page. So my first bit
of code for my website, I’ll be writing
in the views file. So when you open the views file, you can see the Django
has already imported render, but for this case are not be
using the render method. I’ll be using the HTTP
response method instead. So to do that,
I’ll type from Django dot http. Import HTTP response both
the lines are not highlighted because I’m not making use of those functions
anywhere in my program. So to define a new
function in Python, you’ll have to make use
of the d e f keyword after that. You’ll have to specify some name
for your function in my case. I’ll just name it as courses and within this I’ll pass
the request function as a parameter Now All I Want by function to do is
to return a message saying that this is my home
page to do that. I’ll just write return and I’ll be using
the HTTP response function of here and then
I’ll just give an H1 tag. Sorry. I have to specify this
within quotes and I’ll write this is my home page. So now the views component
is ready to display this message on my homepage. But to make this function return what I want I’ll have to connect this view start py file
to the URLs to py file which is present in my project. So to connect the views
not py file which is present in my app to the URLs that py file which is present
in the project. I’ll have to create another URLs
that py file within my app. Now this is done
because I do not want to mess up with the original
URLs to py file which is present in my project. So in case you have
a number of URLs and apps to be returned
the original URL start py file present in my project
can get too cluttered and that becomes messy
and to avoid that. I’m going to create
another URL start py file within my app, which is also
the conventional method. So now this URL stuff py file
is very much similar to the URLs that py file which is present
within my project. So all I’m going to do within the newly
created URL start py file in my app directory is
to import the path function from Django dot URLs. So from Django dot URLs,
I’ll import the path function. So now for the URL pattern, I’ll just copy this
and I’ll paste it in the URL stat
py file of my app and I’ll just delete
this admin URL and I’ll leave this blank now. I’m leaving this blank because I want this function
to be returned on my homepage. Okay. So now what you have to do is specify the views
taught courses function within the URLs dot py file
present in your app to do this. Just type views dot name
of the function, which is courses in my case followed by
the name of your page. You can give any name
of your choice. I’ll just name it as home page. Now as you can see, I’ve added the function
present in the views file in the URL path, but I’ll have to specify from where this function has
to be taken to do that. I’m going to import
the views function present in this directory. And for that I’ll type
from dot import views now getting back to the URL start
py file present in my project. I’ll have to include
the path of my home page. So I’ll create
another path function and within this I’ll specify
an empty string and then use the include function and specify what I want to
include over here. So basically I want
to import this file which is technical courses /
URLs dot p y, so I’m going to specify
that as technical courses. Dottie Waddles after this
I’ll just close this function and close the path function. Okay. So one thing I don’t know if you
guys have noticed I’ve made use of the include function, but I’ve not
imported this function. This function has to be imported
from Django dot URLs. So to import that functional
just right comma include so now I’ll get back to
the development server and see what is returns. Congratulations guys this time
instead of seeing the rocket that was ready to be launched. My website is
displaying the message that I wanted to display. So now your website
is ready to be launched. Okay, so our website
is executing very well, but I want to
show y’all something that’s displayed as a message
for me in the terminal. So as you can see it says
you have 17 unapplied migrations and it says your project
may not work properly until you apply
the migrations for the apps. Now, this is basically
to notify you that your app or your project
has not been connected to the database. So coming back to the project
directory over here. You see something called
as the dpd sqlite3. So this is basically
the default database that is installed
along with Django the settings for this database
are already included in the settings dot py file. So let me just clear
the comments of here. And within the settings
dot py file, I’ll just look for databases. So as you can see over here, the default database sqlite3
has already been installed for us by Django. However, if you wish to use any other database install the
appropriate database bindings and change the keys in the
databases default item to match a database connection settings. However, for a detailed
information on this you can check out
the original documentation where you’ll find all
the details required for the settings of the database
you prefer to use in addition to databases. The settings don’t py file also has installed apps now to show
y’all what that exactly is. So basically Django dot
contributed admin is the admin site Django dot
conducted admin is for the admin site wat is for the authentication
system content type is basically a framework for the content
types sessions is for a session framework messages
for a messaging framework. And static files is a framework
for managing the static files. So now to connect my project
to the database. I’ll hit control Z again
in my terminal and over here. I’ll type the command python -. P y migrate. So now I run my server
again and show you what a displaced this time. So as you can see unlike
before I have got a message saying system check
identified no issues. So this means our project
has been connected to the database successfully. Now what exactly
happens over here is the migrate command looks
at the installed apps settings and creates any necessary
database tables according to the database settings
in your settings to py file. Then the database migrations
are shipped with the app. So now that my project
has been connected to the database. The next thing that I’m going to do is
create models for my website. So to create models, I’ll go to the models to py file
which is already created for me by Django. I’ll clear this comment
of here and as you can see it has already
imported models for me. So within this file over here, I’ll be creating two classes where the first class
will be the all courses class and it’s going to hold
the name of the course and the instructors
name to do that. I’ll just type class. And give the name as all courses
you can give any name of your choice guys. Now within this
I’ll just write models. dot module Now like I’ve already
told you all within this class. I’m going to write
the name of the course and the name of the instructor. So for that I’ll just take
a variable say course name and then I’ll specify
this two models dot since this is going
to be characters. I’ll use the function since this is going
to hold on the characters. I’m going to use the function
care field and for the length of this I’ll just specify as
max length equal to anything say 200 after this. I’ll just write
the instructors name. So I’ll just take a variable say
ins name and again over here. I’ll use the care field method. And I’ll give a max length
of stay a hundred. Okay. So the first class
has been created now, I’ll create another class which is going to hold
the details of each course, so to do that, I’ll just type class
and I’ll say details as the name of this function. So as many of you know, the course types
that are available at Ed Eureka are either self-paced
or instructor-led. So I’ll create
the same over here. I’ll say self-paced. I’ll just say SP and over here. I’m going to do
something very interesting. So before I do that, I’m going to create
another variable say course and using this variable. I’ll connect the subclass to the main class using the
foreign key method to do that. I’ll just type models
dot foreign key. And since this method is inheriting the
all courses method, I’ll just type all courses
and then I’m going to use the on delete function. Now, you all might be wondering
why I’m using this function. Okay, I’ll just tell you that in a while and I’ll just
type models dirt Cascade. Okay, the purpose of using this
on delete function over here is because in case I delete
any course present in all courses section, I want to delete the details
of that function as well. So in case you have
any other website, we’re in it’s not necessary
to delete something present in the detail section. When you delete an object
of your main class, you may omit using the on
delete attribute So within this like I already told y’all
I’m going to have two variables which is for the self-paced
and over here again, I’ll be making use
of the Care field function. So to specify that I’ll just take
a variable say SP and then I’ll use the same
function again just to specify what is present in that course. Now the length
of it can be changed according to your preferences. So in this case, I’ll just change it to 500 so that I can specify all
the topics the second type of course that’s available if the instructor-led courses so for that I’ll just take
a variable say IL and again, I’ll be using the same function and I’ll change
the length to 5 hundred. Okay. So now I’ve created
models for my project. So basically each class that I’ve created over
here is a subclass of the Django dot DB
dot models dot model. Each model has a number
of class variables both my classes have
a few variables Each of which represents
a database field in the model. Now, this field is actually
represented by an instance of the field class. For example, the care field function
that are used over here for characters this
basically tells Django what’s type of data
each field holds. So now talking about
the foreign key function that I’ve used over here. Now this function is actually
used to define a relationship. So it basically Don’t jangle that each detail is related
to a single course in addition to this the small bit of model code that we’ve written
over here gives a lot of information with this Django
is actually able to create the database schema or the create table
statements for this app. It also creates
a python access API for accessing all courses
and the details classes. So now to tell Django that we’ve installed
an app into our website. All we have to do
is go to the settings to py file go to install
the apps and over here. You’ll have to specify
the name of your app which in my case
is technical courses. Dutch apps go to the apps file
over here and from here take the class name and paste it
in the settings dot py file where you’re creating the app
and paste it over here after this just close
the inverted commas and do not forget
to specify the comma now, we’ve told Django that it has to include
the technical courses app after this you’ll have
to run another command. So I’ll just return
the control back to my terminal and I’ll use the command
python managed at py make migrations and the name
of my app python manage that py make migrations
and the name of my app. It has thrown an error. It says type error
in the Cascade function. So I’ll just get back to models. Oh here it is. I’ve not specified
any attributes to this. So I’ll just remove
this from here. So I’ll just clear
the brackets save this come back to my terminal. So here it is make migrations
basically tells Django that you’ve made
some changes to the models and the Django should store these changes as a migration
migrations are basically how Django stores
changes to your models after this you’ll have to use
the SQL migrate command that takes migration names
and returns their SQL. So all you have to do is use
Python managed py SQL migrate and specified this ID
which is present over here. So it’s not not not one. So just type the same
and hit enter. I forgot to specify
the name of my project. I’ll just specify the name
of my project over here. So as you can see, the table names
are automatically generated by combining the name of the app and the lowercase name
of the module. Also, the primary keys
are added automatically now. All you have to do is run
the my great again to create a model table in your database. So here I’ll type python
managed rpy and migrate. So now the migrate command
takes all the migrations that haven’t been applied and runs them against
your database migrations are very powerful guys. And this actually lets
you to change your models over time as in how you
require to develop your project because of this you do not have
to delete your database or tables and make new ones. The reason that there are
separate commands to make and apply migrations is because you’ll commit migrations
to your version control system and ship them to your app. They not only make
your development easier. They’re also usable
by other developers and in the production. So now that my database is set. I’m going to jump onto the interactive
python shell to make use of the free API Django
has given me to open the shell you can simply type the python
manage dot py shell command in the terminal or in
the command prompt of your system. So I’ll just open up the command
prompt again and over here. The first thing
that I’m going to do is get into the directory
where I’m creating my project so to do that, I’ll just CD
into pycharm projects. Flash Django full course, I’ll get into my website
project and hit enter. Okay now I’ll just type
in the command python managed py and I’ll specify shell and I’ll use the shell command. Now I’m doing this
because it’s easy for y’all to see what exactly I’m doing
with the database. The first thing that I’ll have
to do is import the classes that I’ve created
in my models file. So for that I’ll just use
from the name of my app, which is technical courses. Dot models. I want to import the classes that I’ve created
which is courses and details. Let me look back
into the name again so that I don’t make
any mistakes it was models and just close this. It’s not courses. It’s all courses. And details so before moving
on let me just show you all what is present
in the all courses set. So to do that, I’ll just have to type all
courses dot objects dot all so as you can see over here. It has returned
that my set is empty. Naturally. It has to return an empty set
because of not added anything to the courses class yet now
to add something to this class. I’m going to take
a variable say a and then I’m going
to take the name and then I write
the name of the class, which is all courses. Now within that I’ll have
to specify the course name and the instructor name since these are
the two variables that I’ve created
within my class now to do that. I’ll just get back
to the shell and I’ll have to write the course name. Let me just cross check
the name again. So it’s course name
and ins name? So course name. I’ll just say
the course name is python. Followed by the name of the
instructor sew ins name equals. I’ll just take some random name
say XYZ after this. I’ll have to make use
of the save function to save this to the database. Now in case I want to print
out the name of the course, I’ll just have to use
a DOT course name and hit enter. So as you can see when I remove the course name
of a it has returned that the course name
of this is python. So now in case I want
to add in more courses, I’ll just take another variable
say be I’ll copy this. And for the course name, I’ll just name it as Java and
for the instructor name, I’ll just say ABC
after this just like before I’ll have to save it. So I’ll say B dot save hit enter and now just to show y’all
what is present within P. I’ll use B dot course
name and hit enter. So as you can see
when I used a DOT course name, I got Python and when I
used B dot course name, I got Java as my output
now one more thing. I want you guys to know is
Django is going to assign some primary key
to both these courses. So in case I want to retrieve
the ID for Python and Java, all I have to do is hit a DOT ID
or a DOT PK hit enter. So as you can see
when I hit a DOT ID, it has returned
the primary key for python. Now, you have another option
over here in place of ID. You can also make use of PK which will return
the same result. Let me just show that to you all so I’ll just
use B dot PK and hit enter. So as you can see
the primary key of B is 2 and the primary key of A is 1 so now let me show
you all what is present within the database using
the All function. So I’ll just rerun
the command I’ve used earlier which is all courses taught
objects dot all hit enter. So now our set is not empty, but you can see
that it has a return something specifying the primary keys
for both my courses, but this is not exactly
very clear now to have a clear output. I’ll just get back
to my models file and I’ll make a small change
within the classes that I’ve created over here. An override the Str function and then the default parameter
for every python class. So now if you have
a doubt of why self has been added over here, you’ll have to remember guys that whenever you’re creating
a function within the class, you will have to specify
the self parameter. Unlike Java in Python. It’s compulsory to specify
the self parameter. So now all I wanted to return
is the name of the function. So I’ll just say
return self dot course name. I’ll do the same within
the details class as well. I’m for the details class. I’ll just return
the self-paced course for now. So having added this Str method. Let me just tell you that it is very important
to add this not only for your own convenience when dealing with
the interactive prompt, but also because objects
representations are used throughout Django’s
automatically generated. Admin save these changes
get back to your shell and now I’ll just exit this. I’ll type python managed or py
I’ll get into my shell again using python managed
at py Shell command. And after that I’m going
to run the previously used function again, that is all courses
taught objects dot all. Oh sorry guys, I forgot
to import the functions. So first I’ll have to import and then I’ll have
to retrieve it. It has not returned the name
of the course again seems like I’ve made some mistakes. I’ll just get back
to the modules file here. It is instead
of a double underscore. I’ve used the triple underscore
and since I copied that of here, let me remove that save this get
back to my shell exit this one. And then I’m going to hit
the Bison managed to achieve I shall command again. Import my classes and then I’ll use all courses
taught objects at all. Okay. So there it is instead of returning the primary key
of both these courses. It has now returned
the name of the courses, which I wanted it to return
not just this guy’s you can actually filter
out anything you want from the database to do that. You can make use
of the filter command. So for that you’ll use all courses taught
objects dot filter and within this I’ll just
specify the parameter as ID equal to 1 I’ll hit enter. So as you can see
I’ve specified the ID as one and it has filtered out Python
and return the same similarly. You can also make use
of the get method to do that just specify get and within that
I’ll say ID equal to 2. So now the get method has it
returned Java as the name of the course having ID to okay so till now we’ve been seeing how to add and filter items
from the database now, I’ll get back to my pie. And show y’all how you can actually work
with the admin to do that open up the admin
file present in the pap. Now for this. I’ll just open up
the terminal and over here. I’ll just type the command
python managed to py create super user create
super user is one word. So just be careful guys. So python manage dot py create
super user So the first thing is going to ask you is
to enter a username. I’ll just enter the username
as editor acre and hit enter after this. It will ask you to enter
some email address. You can specify
any email address since this is just
a test project. I’ll just say Eddie Rekha
at the rate gmail.com. After this, it will ask
you to enter some password. I’ll just I’ll just use
some password say ABCD. I’ll give some password
of my choice. You can give any password
of your choice guys. Once I’ve entered that I have
to re-enter the password. So now once the super user
has been created you will see this message saying super
user created successfully now, all you have to do
is run your server again and for that you’ll have to make
use of the Python managed to py run server command. Okay. For this is my home page now to go to the admin section
within the address bar. I’ll just type slash. Admin. Okay. So here it is. The Django Administration site
has been opened now, make sure you remember. What was the user name you
entered and the password. So my username was at Eureka
and I’ll just enter the password that I had saved. Once I’m done with that a login, so as you can see over here, the Django Administration site
has opened now to make my app modifiable in the admin. I’ll have to import that into my admin
dot py file to do that. I’ll get back
to my admin dot py file and within this admin
dot py file I’ll type from dot models. Import the all courses class once I’ve done this
I’ll type admin dot sight dot register. And the name of my class
which is all courses. I’ll save this and I’ll get back
to my development server and I’ll refresh this page. You can easily see the changes
I’ve made to my project. The all course is
class has been added to the admin section. Now when I click
on this you can see that both the courses that I had added in the database
are shown up in my website now in case you want to add
in any other course, you can use the add
all courses option over here and specify some
of the course name this time. I’ll just say see
the instructor name. I’ll just use XXX
I’ll save this. So as you can see the course that I’ve added is C
and that has been added to the all courses option
in my website. Not just this
from the admin site. You can delete any course
of your choice, but just clicking on that
and using the delete option that’s present over here. So I hope you all have noticed that this form has
automatically been generated from the all courses module for I hope you are clear to all that’s been explained still
here now get back to pycharm. I’ll open the views file and I’ll modify this view is
basically a type of web page in your Django application guys that generally serves as
a specific function and has a specific template say, for example, you have
a Blog application. So in the blog application, you will have something
called as the blog homepage. So for that you’ll have a Blog homepage detail page
Etc now for my application, which is technical courses. I’m going to include
the home page and The Details page. Fun open up the views file
and over here. I’ll Define another function
and I’ll name it as detail. I’ll have to pass in
the request parameter and within this I’ll
specify another argument to this function which will be the course ID. So course underscore ID. And again, I’ll write
a return statement. So return HTTP response, so I’ll just create
h2 tag at this time within quotes again. So H2 I’ll say that these are
the course details just close the h2 tag again. I’ll just save this now. I’ll have to go
to the URLs dot P wife. I love my app and within this
I’ll have to include the part for my Details page. So for that I’ll just write path which is the name
of the function now over here. I’ll just specify that in t should be
the course ID which I specified earlier. And I’ll have to end this
with a backslash following this I’ll just have to write
views detail view stir detail and I’ll give a name
to this saying that this is the details page. I’ll close this function
and write the comma and not to forget the comma. Let me save this. I’ll get back to my server
and over here. I’ll go to my homepage. Sorry guys. I’ve made a mistake of here. It’s views dot detail save this
and I’ll rerun my server. Okay, so now I’ll just
specify the idea over here. I’ll say the IDS
1 and hit enter. Okay. So as you can see over
here now it is returning what is present
in my Details page. So as you can see over here,
I’ve specified the ID as one which is the primary
key for Python. And when I hit enter you can see that it has returned
what I had specified in the details function now, let me give some primary key that is not at all present
in my database say for example 5 and hit enter. So as you can see I
had only two courses which had primary key one and
two but when I’ve entered five even then it has returned
the same page now. If you don’t want
your server to do this, you’ll have to include the HTTP for not for function
in your Details page. So now let me Define a little
more to this function instead of returning the these are
the course details. I’ll include the idea
along with this. So I’ll just use for Course ID
and over here just close this. I’ll add the string. Of course ID. Please make a note that I am using
the same spelling. I’ve used everywhere now. I’ll save this and I’ll rerun
my development server. Okay. I’ve just made a spelling
mistake over here. However, that’s just HTML
so I can change it anywhere. So having done this. I’ll get back to my pycharm
to create some templates for my website. So before moving on
towards templates, I’m going to make
a small change over here. I’ll just remove this. I’ll create a variable
for all courses. So I’ll just name it as DC and within this I’ll
save all the courses that are present in my database. So all courses
the objects dot all. Now I’ll create another variable
set template and I’ll use the loader function. And get underscore template. Okay, so I forgotten to import
the load of function. I’ll import it over here. So from Django dots templates. from Django template import loader So now
it should be fine. So now to create the template
I’ll right click on the app that I’ve created
choose new directory and I’ll name it as templates. Now within this templates
directory, I will create another subdirectory which will have the same name
as my application. So I’ll name it
as technical courses. So if you’re wondering
why I’m doing it this way, it is just the standard
convention to create templates within your code. It also makes it very easy for Django to look for templates
within the app itself. So now within the sub directory,
I will create an HTML file. And since this is
for the courses page, I’ll just write
courses dot HTML. So this is
basically my homepage. Also guys, I’ll just clear
this and I’ll be here I’ll just create a list say
ordered list within this. I’ll just write
some python code remember that whenever you’re
including python code within HTML you have to do it
within the curly braces and the Ampersand symbols just as shown on screen. I’ll just make use
of the for Loop. So for X in all courses So
within this I’ll create a list and not just this
I’ll create a link as well. So I’ll link this. So I’ll use the HR function. So for that I will have
to use the anchor tag and hrf now over here. I’ll have to specify the name
of my application. That’s technical courses. flash goes ID and then
I have to close this. Now I’ll just link this
to the name of the course. So I’ll use all courses
dot course name. Okay, so that’s it. Finally I’ll have to end
for so to do that. I’ll have to do it
the same way I’ve used for So within the curly braces
do percentage symbols, which is going to contain
the end for statement. I’ll see if this Okay, so now I’ll get back
to my views function and over here. I’ll have to specify
the path of my file so which is within
the technical courses technical courses directory and the name of my file
is courses dot HTML. So over here, I’ll create another
variable named context. The context is a dictionary
mapping template variable name to python objects So within this I’ll have
to store the dictionary of all the elements
or all the courses that are present. So that’s a see. And now coming towards
the return statement. So I want to return
the template dot render and that will be context and the request so now my views
files seems to be ready. I’ll get back
to my development server. Looks like something
is missing over here. I have the list but I
do not have the items. So let me get back to my courses dot HTML file and check
what mistake I’ve made. Okay. So here it is. I’m using the for Loop over here
where I’ve used for X in AC. So now to actually return
the items instead of all courses and supposed to use x
dot course name. So now let me save
this back to my server and I’ll refresh this page. Okay. So here it is as you
can see all the courses that are present in my database
are returned on my homepage. So I hope you all
have understood to here. So now what if the user
requests for a course that does not exist
on my website in that case. I actually want to Is a for not for error many
of you might be aware of the for not for error
it is it is the very famous the page not found error. Django actually has
a function named HTTP for not for which will allow
you to raise this page not found error in case
the user requests for something that’s not present
on your website to do this. I’ll be using the try
and except blocks. So first I’ll write
the try block and within this I’ll create
a variable named course, I’ll store all courses. So all courses dirt objects and here I’ll be using the get
method with the primary key. So PK is equal to the course ID. Okay. So after this comes
the except block, so here I’ll be raising
the does not exist except method to do that. I’ll just write
all courses dot does not exist. Be careful with
the spellings again, and then I’ll just
raise the http. Phone out for error. I’ll specify some message
saying course not available. So now I’ve actually
not imported the HTTP for not for method. So I’ll just import
it over here. So HTTP for not for
okay so looks cool. I’ll have to change this. I’ll change the return method
from HTTP response to the render method. This is actually a short
cut method you can specify the request parameter and the link to the details page
as well as the dictionary as a parameter to this function. Okay. So just clear this up and first I’ll have to return
the request then I’ll have to specify the path
of my Details page. So it is within the
technical courses template and the name is Details page
so detail dot HTML, so I’ll specify the course. Okay now I’ll see if this now I’ll get back
to my details homepage. So my variable name is
not a see its course. So of course, let’s save this get back
to my development server and over here. I’ll just specify
the primary key for python which was 1 and hit enter. So as we all know the primary
key for python was one and when I’ve specified
the primary key. It has returned python, which is actually present
in my Details page. So now let me give some other
ID and see what happens. So I’ll specify
to so the primary key for Java was too and therefore
when I’ve specified that I’ve got the output
as Java over here now in case I specify some ID that’s not present
in my database say for example, 45 and hit enter. So here it is the page not found error has been raised and it
says course not available. So I hope everyone’s
understood till here. So now getting back to pycharm. So I had just written this
in the details homepage for y’all to see Happens
when I do this now. I want to define something else
also in my details homepage. So before defining
our detailed HTML page, what I’m going to do is go get
back to the admin dot py file and over here. I’ll register the details class
also at my admin section. So here I’ll use
admin dot sight dot register. And the name of my class
which is detailed. Okay, so I’ll have to import
it over here first so details and then details, okay, and I’ll save this I’ll get back
to my development server. So now I’ll get back to
my admin section and hit enter. So as you can see
over here previously in the technical courses section
only one option was shown which was all courses now, I’ve specified the details and now I’ve added
the details class as well. Now when I click
on this you can see that there are zero details, which means we had not entered
anything in the database to add something to The Details page
you can do it in the database or over here directly. So what I’m going to do is
add some details to my page, I’ll choose the course
type to be python. So here I’m just
going to describe what the self-paced
course actually means. I just tried self-paced course
precedes based on the learner. For the instructor-led course, I’ll just write the course
will be taught by an instructor. I’ve made a spelling
mistake hits instructor. Okay, maybe that’s
a very basic information. But since it’s just
a test website, I can enter anything
of my choice. You can enter anything you want. So I’ll just save this. So as you can see over here,
I’ve added some details. The reason why I’m seeing
this over here is because I’ve asked my function
to return the self-paced course now to change this I’ll get
back to my function. So that wasn’t my models file. So I’m going to be making
a small change of here instead of returning the details
of the self-paced course. I want to return
the primary key of my courses. So that would be PK and this has to be
within a string. Okay, so I’ll save this now
I’ll go from the terminal. So I’ll get back to my server. I’ll just click on the link. So I’ll go to the
admin section again. Okay, I’ll click on details. So as you can see instead
of returning the details of the self-paced course, it has returned
the primary key of python, which is 1 now. I’ll click on this and
as you can see over here, it has returned that the course is Python
and Test 2 types, which is self-paced
and instructor-led. So all this is working on the back end of your project
the front end basically deals with HTML python actually
works in the back end of your websites. So now coming back
to our Details page. What I’m going to do over here
is print out the course name the instructor name as well
as link the course name to the course landing page
of Ed Eureka to do that. What I’m going to do is
use the if else statements. The first thing
that I’m going to do is write some python code with an HTML. So here it will be if course dot p
k is 1 which means if it’s python then I want
to return the course name and I want to make this an H1 tag and then
I’ll create an anchor tag to which I’ll link the course
landing page of Erica. So I’ll just open up the browser and I will take
the link from here and I’ll paste it
over here after this. I’ll just close this. And the course name, I’ll have to close the anchor
tag and close the H1 tag. So there it is. Now. What I’m going to do
is return the name of the instructor as well. So of course dot ins name, please be careful
with the spelling’s use the same spellings that
you’ve used for each variable. So now we’ve done for Python and now we have to do
for C++ and Java, I’ll just return the name
of the instructor in h2 tag, so that it’s more visible all
this is your choice guys. You can make it. However you want. And now what I’m going
to do is use the else if statement so to percentage signs close
the curly braces and over here. It will be LF course dot PK is to what I’m going to do is
copy this It stood over here and I’ll just change the link. I’ll take a Dura Costco’s
landing page of java and I’ll just copy this
and I’ll paste it over here. So now similarly you
can do for C++ as well. I’m just showing to observe. I’m just going to do it
for Python and Java. So I’ll just copy this again and I’ll paste it over
here then for C++. Then for the else statement, I’ll just write
the course name of C++. I’m not linking this because I’ve already
shown you two examples. So course name
and the instructor name that will be
in the H1 tag again. Sorry. So H1 course name
and close each one after that. The course instructor
named just copy this one. So now I have to end this and I have to end it
in a similar way that I’ve been writing
the else if block so it will be end if okay now I’ll save this so I’ll get back
to my homepage and over here. Alright / primary
key of python, which is 1 and hit enter. So as you can see over here when I entered
the primary key of python, which was one earlier. It was just showing
the name of the course. Now as you can see, my course name has been linked
to the official website of Erica similarly. If I do for two and three,
I’ll get the same. But if I hope you’ve understood
this so you can add on whatever details
you want to your page. Just like how I’ve shown you. So now coming back to pycharm. I want to actually
link my courses page to my Details page. Now, what I’m going to do
for that is actually I’ve made a mistake
of here in the reference. I’ll have to remove
this and over here. It will not be course ID. It will be x dot ID and that is
what I’m going to link. Okay, so x
dot course name now, I’ll just save this. I’ll get back to my server
refresh this page and I’ll click on python. So as you can see over here, I’ve linked my home page
to The Details page now if I click on this as I’ve already
shown you it will go to the course landing page
of Eddie Rekha. So now we’ll get back
to my development server. Now as you can see, I have my back and being
displayed on my front end. So now what I’m going to do
is add on some more details to my homepage if my home page does
not look beautiful. I don’t think many people
would even like to visit my website now basically
beautifying your home. Is depends on how you use your
HTML and CSS now to do that. I will get back to course
dot HTML page and over here. I’ll write some text as H1 tag and that will be welcome
to edu recur after this. I’ll just add on some image. So I’ll just add on
and Erica image over here. I have the source link. So I’m just pasting it
over here guys after this. What I’m going to do is close
this image tag, obviously. Now, let me save this and get
back to my server and see what happens when I
refresh the page. I refresh this. So there it is guys. Welcome to edu Rekha
and the edge Erica image. So now to add some more
details to my homepage. I’ll go to the official
bootstrap website and I’m going to copy paste
some part of this code. So I’ll just copy this guy’s
bootstrap is very good in helping you create
your HTML web pages. So just to beautify my page. What I’m going to do
is add this right in the beginning of my code. Let’s save this. I’ll get back to my website
and I’ll rerun this. Okay. So I hope you see
the changes our website before was looking very dull and now it looks
much more beautiful compared to what it was before. Okay. So my website as of now does not have anything
such as a menu bar or something. So let’s move on and add that so I already have
a piece of code for that just copy it. And I’ve bested this is welcome
to I do they go. Okay. Sorry for that. So I’ll just add it here. Let’s save this and I’ll rerun
my development server. Okay, as you can see over
here a menu bar has been added to my homepage. Okay. So all these bits
of codes are taken from bootstrap website. You also can take it
from there guys. So as you can see over here, my Details page
looks too plain. So to make it more descriptive
I’ll be adding on some images and we’ll create a form for the user to select
between self-paced course and the instructor-led
course options to do that. I first need to make changes
to the models to our py file from my project. So I’ll get back
to my project and over here. I’ll open up
the models dot py file. So the first thing
I’m going to do over here is add a new variable that will specify the date from when the course
has been started. I will name it as started from and we’ll use
the date and time field of the models class. So to the all course is class. I’ll Adding a new variable named as started from started
from and I’ll be using the models dot daytime field. Please be careful
with the spellings. So that is date daytime field and within this I’ll
say started from so once I’ve done this, I’ll add another custom method
to my all curses class that will be was
published recently. Now, this is going to describe when my course
has been published. Okay. So now I’ll just
Define the class. Let’s say was
published recently. So I want this class to return when my course
has been published. So for that I’ll just write
return self dot started from and this can be greater
than or equal to X owned now. – date-time the time Delta. So one thing I want
you guys to know is that time zone dot Now function is present within
the Django dot utils module and the time Delta
method is present within the date-time module. I’ll just specify the number
of days to be one. So if you want more information
about the daytime module, you can check out
any records videos and blog on the same. Okay, so here I’ll
need to import it. So I’ll just import date time
and from Django Dot utils. I’d have to import. Time zone. Okay. I’ve forgotten the
space over here. So that will be time zone. Okay, so are all
courses classes ready. And now in the details class. I’m going to
remove the self-paced and the instructor-led variables and I’m going to replace it
by a single variable that will hold both the values. So I’ll just clear
this from here. I’ll create a new variable. I’ll just name it as CT
which will be the course type. And since this is going
to hold characters, I’ll use the care field method. Okay, so I’ll just
specify the max length of this to be anything say 500. So I need another variable which is going
to store the option that is selected by the user. So for that I’ll just
name it as your choice and then I’ll be using
the Boolean field present in the models class of here. So that’s Boolean. Okay. Yeah, Boolean field
and the default. I’ll just set it to be false. Now instead of returning
the primary key. I want to return
the course type. So I’ll just change it. So yeah, okay. So now my models are ready now, once this is done, I’ll have to make these changes
using the make migrations and the migrate command. Okay, so I’ll just
open up the terminal I’ll return the control back
to the terminal and over here. The first thing that I’ll have
to do is python -. P why? Mick migrations followed
by the name of the app, which is technical courses. Please be careful
with your spellings. Okay. I’ll just hit enter of this. I’ve just made
a spelling mistake. Sorry guys. Okay, so that’s
technical courses. So as you can see over here,
it’s asking me a question. If I’m if I’ve renamed the details dot il-2 details dot
c t so I’ll just say yes, I’ll hit enter. So now after this since I’ve added a new Field named started from it’s asking
me to provide one default value or to quit and let me add
a default value in the models dot py file itself. I’ll just choose the first
option and I’ll hit enter. So now to set the time I’ll just
use time zone dot Now function which is going to set
the current time. So that’s time zone dot now
and I’ll hit enter. Okay. So as you can see
my tables are ready again, and this time I’ve got a new ID, which is triple zero to so
now I’ll have to use Python – turd py migrate. And I’ll hit enter. I’ve made a
spelling mistake again. That’s my great. So after I’m done with that, I’ll have to use Python
managed py SQL migrate and I’ll have to specify the ID which was Triple zero to
followed by the name of my app, which is technical courses. Once I’ve done this,
I’ll hit enter. Sorry guys. I’ll have to specify the name
of my app before the ID. So that’s technical courses. Followed by the ID which is triple zero two
and then I’ll hit enter. Okay. So now my tables are ready. The next thing that I’ll have
to do is open the python shell so for that I’ll just open up
my command prompt and over here. I’ll have to get
into the directory which holds my project. I’ll just copy
my project path from here. And I’m going
to paste it up here. Then I’ll get into my project
which is website. And after this I’ll have
to write python managed at py Shell. Okay. So by running the make
migrations command, you’re telling Django that you’ve made some changes
to your models. And now you like the changes
to be stored as a migration. So migrations are basically
how Django stores changes to your models and thus
to your database schema. They’re just files on the disk. You can read the migration
from your new model. If you like. There’s a command
that will run the migrations for you and manage your
database schema automatically and that is the migrate command
the SQL migrate command takes migrations names
and returns their SQL. Okay, the table names are automatically generated by
combining the name of the app and the lowercase name
of the module. So in my case the names
were all courses and details so correspondingly
in place of capital A. I’ll have a small a and since my details class
already has a small D. It will remain the same. Another thing I want
to guys to note is that the SQL command doesn’t automatically run
the migration on your database. It just prints it on the screen so that you can see what
SQL Django thinks is required. It is useful for checking
what Django is going to do or if you have
a database administrators who require SQL
scripts for changes. So for some more information
about the migrate command, it basically takes
all the migrations that haven’t been applied and runs them against
your database essentially synchronizing the changes
you made to your models and the schema in the database migrations
are very powerful guys, and they let you change
your models over time as you develop your project without the need to delete
your database or tables and make new ones. It actually specializes
in upgrading your database live without losing data. So now coming back
towards our database. I’ll just show you
what’s present in my database. So for that I’ll have
to use the all function. So I’ll just type all courses. So all the objects present
in the all courses class will be retrieved. I have to first import it so from technical
courses dot models. I’ll have to import
all courses class. And the details class. I’ll just check
if my spelling’s are correct. So that’s details
and all courses. Okay. It’s not and over here. It’s a comma. So after this, I’ll just run the command
all courses taught objects. Not all so as you
can see over here. I have three objects, which is python
Java and C now. Like I’ve already shown
you before in case I want to retrieve just one object
which is present in my class. All I have to do is type
in the same command, which is all courses taught
object and instead of the all method. I’ll have to use the get method and I’ll just specify
the primary key to be one and then I’ll hit enter. So as you can see the object whose primary key
is one is python. You can also make use
of the filter method to do that. Just change the get method
to filter and then hit enter. Okay, so as you can see the get
just like the get method the filter method
has also returned python when I’ve specified
the primary key as one now. I will be importing the Django dot utils time
zone method over here. So from Django dot utils
import time zone, please be careful
with spellings. Okay. Sorry for that. I told you guys to be careful and I myself made
a mistake of here. So that’s Django. Okay, after this
I’ll take a new variable which will after this. I’ll take a new variable named
Oscar named as current year and I’m going to equate it
to the year of the current date. So for that I’ll just write
current on the score here equals x own dots now. Dottie oh after this,
I’ll take the all courses class. And I want to fetch the objects that have been published
in this year. So I’ll use the get method. I’ll use the current your object
that I just created and I’ll equate this
to start it from object that I’ve created
in my class to this. I’ll just specify
underscore underscore here because I want
to retrieve the year. And then I hit enter I’ve made
a spelling mistake. Sorry for that. It’s started from So
as you can see over here, it says get has returned
more than one objects from the all courses class. Now, this is because I had set all
the courses starting date from the time zone
dot Now function, which means I had set it to the current time
for all three courses. So now I’ll take a new variable
Q within which I’m going to store one of the objects
present in the all courses. So it’d be either
python Java or C. I’ll just specify
that through the primary key. So that will be all
courses dot objects dot get I’ll use the primary key as one
itself and then I’ll hit enter. Okay. So now if I want to check if this course
was published recently. So for that I’ll just type
Q dot was published recently, which is the name
of my class, which I’ve defined already. So was published
On the school recently and then I’ll hit enter. Okay, so looks like I’ve encountered
an error over here. It says can’t compare
date time the date time to date time dot date. Okay. Looks like I’ve
made some mistake within my models over here. So let me just check. I’ve imported the data module and I’ve imported
the time zone method as well. So as you can see over
here instead of making use of the date-time field. I’ve made use of the date field and that is why it
is throwing an error of here saying can’t compare
date-time dot date time to date time dot date. So let me just make
that change over here. It states Time Field. I have to save this. So since I’ve made
a change of here, I’ll run the make
migrations command. And after that I’ll have to use
the migrate command as well. Okay. So after this
the SQL migrate command, of course and the ID that is generated
this time is triple 0 3 so now my database
is ready and over here. Let me just exit this
and I’ll clear the screen. So now again, I’ll use Python
managed or py shell. And I’ll have to import. I have to import time zone. Sorry for the mistake guys. I use the command
which I used previously and I had made a mistake
in the spelling over there. Okay? I’ll also have to import
the models present in my app so from technical courses. Dot models, I’ll have
to import the classes which is all courses
and details after this. I’ll have to import
the daytime module. So import Dave time. Okay now just to
check my database. I’ll just run the command all courses dot objects
not all and I’ll check what it returns. Okay. So now my database is ready. So getting back to
where we stopped earlier. So I had just created
a new object q and I had stored
the element having the primary key as one into it. So I’ll do it again. And after this I’ll use the
was published recently method. She looks like we’ve encountered
another error over here. So I’ll get back to my models. And it shows that there’s some problem
with this variable over here. So what I’m going
to do is just remove this variable for a second
and I’ll just comment these out I’ll just open up
my terminal again and I will make
migrations to this then I will use the migrate
command and I’ll use SQL migrate again. So what’s the IDS IDs
triple zero eight? Okay. Okay. Now I’m going to add
on this again. So that doesn’t show
me an error again. I got to make migrations again. The reason why I’m doing this
is previously it was showing that this variable
of mine is of none type. But actually we have to equate
it to the current time, which is x 1 dot Now function. So just to not encounter
the same error again, I deleted that variable or that element from my table
and I’m adding it again so that it’s created
as a new variable. So now I’ll add the started
from variable again, I’ll save this and as
usual I’ll have to make the migrations and I’ll have
to migrate it again. The option over here. I’m going to choose is one hit enter use time zone
method time zone dot now to be specific and
hit enter after this. I’ll have to migrate. And then use
the SQL migrate command. So the idea that this time is 9
and I’ll hit enter. Okay, so now my table is ready. So as you can see over here
previously, it was just showing me the started from name and now it’s showing
me the date and time. So now I’ll get back
to my command prompt. Okay, so now I’ll get
back to command prompt just exit from here
and I’ll clear the screen. I’ll get into my
python shell again. And after this I’ll have
to import everything that’s required which
is timezone method from Django Doodles. The models file from technical
courses website or application and then the daytime module. Okay. So now I’ll take
new variable say and I’ll say all courses
dot objects dot get and I’ll use the primary key as one. Now I’ll use the was published
recently method and see what it returns was published. recently So as you
can see over here, it says that the prime that the object
which holds primary key as one which is python
was published recently and it says this is true. Okay. So I hope everyone’s
clear till here. So one thing
that’s very important that we learn to be here is while making changes to your model you will have
to make migrations and then migrate it and then use the SQL migrate
command with the ID that is generated by
the migrate command. So any time you make changes you will have to do all
these three things. Even if you make some mistake within the models
don’t be scared because each time you make
a mistake you get to learn even more from that. So as we all know
my details class is interlinked to my all courses class, which means my details
class is derived from my all courses class. Now this connection
is actually made through the primary key and
the foreign key relationships. So before making
any more changes of here, what I’m going to do is get back
to my development server and over here. I’ll go to the
admin section now, I’ll get into the all courses that are present
on my website and over here as you can see I
had three courses which was C Java and python now when I click on one
of these you see that previously only
the course name and the instructor name was
shown but now it also specifies when my course has been started. So as you can see over here
previously you could see self-paced and instructor-led
courses over here. So now what I’m going
to do is click on this and I’ll just clear
this from here. So I just killed the details. So now I’ll go to my database. I’ll exit this and I’ll have
to re-enter my shell again. I’ll import my classes again. And I’ll use
the previous commands Okay, so now I’ll just take the object a and I’ll say details
on this course set dot all. So as you can see over here, I deleted the details that were present
from my admin section and that is reflected
over here as well. So what I’m going to do now
is set to values for my variable which was CD that is
course type to do that. I’ll use a DOT details
on this corset and instead of
the all functional use the create function
with the help of this create function. I’ll be able to assign
one or more values to the curse type field. That is CT. So I’ll say CT is
equal to self-paced. Okay, so that was my first
option and now I hit enter. Okay. So one option I’ve already
created which is self-paced. Now. I have to create another option
which is instructor-led. So for that I’ll use
the same command again and I’ll just change from
self-paced to instruct a lit. And I’ll hit enter. Okay, so now I’ve
given two options. But when I gave the first
option it will turn that the details are self-paced and when I gave
the second option, it has a return that the details
contain instructor-led. Now if you want to see if both these options are stored
in the detailed set. All you can do is make use
of the count method. So instead of this
all method of here, I’ll use a tooth details dot
count and I’ll hit enter. So as you can see
it has returned that I have to details
for this object, which is python which means python
has two objects, which is self-paced
and the instructor LED options. So I’ll repeat this
for another course, which is present
in my database say for Java. So I’ll take a new variable
this time and instead of a I’ll rename this as B
and the primary key I’ll specify as to so now let me
just retrieve the course name. For this primary key, which is 2 so now
as you can see the curse name, which is present at primary key. Number two is Java now we’ll set
the options for this as well. So I’ll use B dot details
on this corset dot create and I’ll give
the same options again, which is CT is equal to
self-paced and another option, which I’m going to give
is instructor lid. Okay now I’ll hit enter. So now let me give
the count option for be as well. So as you can see, I’ve created two
options for Java which is instructor-led
and self-paced courses. Okay. So I hope everyone
understood till here. So now I’m done with the models and I’ve done
with the database now, I’ll have to modify my viewers
and my templates so to do that, I’ll go to My Views file
and over here. I’ll make some modifications
with regards to the changes. I’ve made to my models
so over here, I’ll be creating a new function
named your choice that will hold the users choice
and we’ll save it after the user has selected
that particular type, of course he intends to enroll for so here I’ll be making
use of a new function which is present in
the Django dot shortcuts module and that is the get object
or for not for method. So for that I’ll just
import it over here. So that will be get object
or phone out for since I’m not made use
of this it’s not Highlighted so like I’ve already told y’all I’m
going to create a new function which will be
the your choice function. Now this function will actually
store the user’s choice and it will reflect the same after the user
has made his choice between instructor LED
and the self-paced options. So now I’ll be
defining the function. So it’s D E F your choice. And as usual, I’ll have to pass
the request parameter. And I’ll have to specify
the course ID. So within this function I’ll make use
of the get object or the for not for method to do that. I’ll just take a new
variable say course and I lose get underscore object
not for not for method and I’ll specify all courses. And the primary keys. Okay, I’ll just
change this to course and we here I’ll say
course underscore ID after this I’ll be making use of the try
and except block. Now. This is in case
some course is not present. I want to catch that error using
the try and the except block. So I’ll just use
another variable over here. I’ll say selected selected
course type equals details on this corset dot get And I lose the primary key
equals request dot post method. And within this I’ll just
specify this as the choice. So now done with the try block. I’ll have to go to the
except Block in case there is a key error. I’ll use the method key error and all courses dot
does not exist. Now make use of the return statement
with the render method. Remember guys that the render method
is always a shortcut to the HTTP response
method So within this the request parameter again, and then all courses /
details or detail dot HTML, which is the name
of my target file after this as a specify
the dictionary which holds what to return for course
and for the error message and now the error message error
on Disco message. So the error message I
want to display is select a valid option in case the user
provides some invalid option. So that will be
select valid option. Okay, so in case
the user does not make a selection and hit
the submit button. It’s going to return a message
saying select a valid option. So after this, I’ll have to use the else block
and then if in case he’s made the selection selected
course type dot your choice. when the user has
made some selection, I want to set the value of the
selected course type to be true and then I’ll just save
this using the save method so selected selected
course type dot save And then I’ll just make use of
the return random method again. Now here it’s going to return. I want to redirect My Views
to the templates, which is the detail
dot HTML so for that. So technical courses / details dot HTML now
the dictionary that specifies my details again. So that’s course
and the value is course itself. So one thing I forgot
to do over here is import the details class as well. So I’ll import that and one small change
within the detail class. I’ll just clear this. And over here, I’ll just say course
equals get object or for not for and over here. We all courses
and the primary key, which is equal to the cosine t
So I’ve made use of cou SE and not cou RSE and everywhere. I’ve been using
the same spelling. So just be careful guys in case
you’ve given some other spelling then be careful that whenever you’re referring
to that variable. You give the correct spelling that you’ve mentioned
in your app. So after this
the return statement again, so return render and request
which is the first parameter and it’s and then
technical courses. slash detail dot HTML
And then the dictionary again, so I’ll just copy paste it from here. I’ll just clear this method
which I’m not been using. Okay, and actually
I’ve not made use of the details class as well. So I’ll just remove it. Looks like I’ve missed
something of here, which is the course dot details
and this corset dot get so here. There’s another change as
this cross-checking my file and I found another change
which is technical courses. So now I’ll just see
if this okay. So now everything looks perfect. So once My Views are ready, I’ll have to go towards
the templates now the page which I’m targeting over here
is the detail dot HTML page. So I’ll just open up
the detail dot HTML page. So as you all know previously, I’ll just get back
to my development server and show you
what this page shows. So here it is the detail start
HTML page basically Returns the name of the course
with the name of the instructor. Now this looks too plain. So I want to add on something
that is so basically what I’m going to do is add
on an image of this course, and then I’ll be creating a form for the user to select
between the self-paced and the instructor LED options. So now to do that,
I’ll get back to pycharm. And over here, I’ll just insert some images
for my courses. So the first thing
I’m going to do is make use of the image tag and the source. I already have it somewhere. So I just copy it. So I’ve just copied the link of
a python logo here after this. I’ll have to create
the list of options present within this course, so to do that, I’ll just take an unordered list
and within that I’ll use Python code again. So I hope you all remember how we have to specify
python code with an HTML. It is between two curly braces
and to Ampersand symbols. So that’s 4X in all courses dot set dot
all I’ll say create a list and I’ll say x dot c t
that’s course type. And for okay, I’ll see
if this and I’ll just show you what changes
I’ve done over here. So I’ll get back to python. Okay, so as you can see
previously it was just showing me python which is the name of the course
and the instructors name. Now I’ve added on an image and it shows me
the image over here. Now. My details dot HTML page
is still not complete. So for that I will
be creating a form. So as you all saw I made two changes one
is adding the image and the other change
was adding a list that contains the self-paced
and the instructor LED options. Now you would be wondering
why only one change is being reflected
on the development server. Now this is because I’ve not made
changes to the URL. So as you all have seen
I’ve made two changes to my details dot HTML page, but only one was being reflected
on the development server. Now this is because I’ll have
to make changes to the URLs dot P Wi-Fi as well. Now before doing
that I’ll just complete the form that I was going
to create on this page. So for that I’ll have to include
some more python code over here and this time I’ll use
the if statement. so if error message then
I’ll just give a paragraph tag. And I will give this a strong. And to this I’ll specify
the error message which I’ve previously defined. Now I’ll just close
the if statement. So that’s endif. So now to create a form
I’ll be making use of the form tag present
in HTML many of us would know every form has
an action to which I’m going to specify the your choice
function present in views. So for this I’ll open
the curly braces and within the Ampersand symbol, I’ll say URL and I’ll
say technical courses. Now the reason why I’m going to use the technical
courses name over here is because this is the name
of my app and I’m going to be using this name space for all the URLs
present for this app. I’ve not made that change as
of yet in the URLs that py file so when I’m doing that you will understand this
in a better manner. So that’s your choice. And then I’ll specify
the course ID then I’ll just close this. So the method that I’m going to specify over
here is post method equals post. So I’ll just close
this over here. So now I’m going to be making
use of something very special which is the csrf token. So that’s csrf underscore token. So before explaining this
to y’all in detail, I’ll just complete the code
and then get back to it. So now we’ll be making
use of the for Loop. so far choice in course
Dodge detail on this corset dot all so for all the elements
present within the detailed set. I want to create an option for the user to select
between the self-paced and the instructor-led courses. So here the input type
input type will be radio and the name is going
to be Choice. I’ll just name it as choice
and ID will be choice and that is going to be
for Loop dot counter and the value will
be the choice ID. So now I’ll just create
a label tag for choice. for Loop counter again
And I’ll close this. So the text that I want to display
over here is the choice underscore CT which is
the course type Choice dot c t which is the course type. Okay, so close the label tag. And I’ll create
a line break over here. So let’s be our now. It has to be
within the Ampersand. Okay, so I’ll just save this and I’ll create
an input button over here. So that’s going to be
input type will be submit and the value also
is going to be submit. So now I’m done
with the detail dot HTML page. But before moving on
towards the development server, I’ll have to include this
within the URL study py file that I’ve created
for the technical courses app. The URL start py file can have
a number of URLs for each app that is created now
to distinguish between each app. It’s better to give
a name to that app. So that the URL
specific to that a par with in the name of that app
itself now to distinguish between each of them. It’s better to give
a name to each app so that the URL specific to it
will be within the name of that app itself. So I’ll just name
the app of here. I’ll say app underscore name is
the name of my app, which is technical courses. So after this is done, I’ll have to
create another path. So I’ll just copy this
and within this the page that I want to refer
will be your choice and that will be with regards
to the primary key that we’ve already created. Okay, so just copy that and I’ll say your choice
which is the name of my function and then / ok, so that will be
views dot your choice and the name will be
your choice again. You can give any name
of your choice just like before so now
my file is already. The first thing that we did was made
changes to our models and then we stored
the changes in our database after making the changes
to our models and database. I was supposed to make
changes to views which I’ve done already
and then the templates so that is details dot HTML page. Finally. I’ve linked the details dot
HTML page to the URL stood py file present within my app. Okay, so I’ll just save everything and I’ll
rerun My code to see if this and I read on my server. Okay my development so it doesn’t seem
to be running. Okay. I’ve made a mistake of here. It’s your choice not hoist. So I’ll just see if
this I’ll get back to my server and I’ll rerun this. So looks like we’ve
encountered some error. So let me just get back
to my Details page and over here. I’ll just cross check what I’ve done so
it’s a link error. So I just have to go to the link and check what mistake
I’ve done over here. Okay. So that’s URL technical
courses: your choice. Oh here it is. Instead of typing course dot ID. I’ve typed course underscore ID. So I’ll just say course
dot ID save this will get back to my development server
and I’ll rerun this. Okay. So congratulations guys. Our form is ready. So when I select any
of these options and I hit on submit
it is going to save that as my option. So whenever I login next time it will just show me
the option that I’ve enrolled for and it will not show
me the other option. So before doing that I’ll just explain
to y’all what’s happening on this detail about HTML page. So our details dot HTML page
has created a radio button for each option that is Well spaced or instructor-led the value
of each radio button is the associated
course type ID the name of each radio button
I’ve given is Choice which means when somebody
selects one of the radio buttons and submits the form it will
send a post data saying Choice which is equal to Hash where hash is the ID
of the selected choice. Now, this is the basic concept
of all HTML forms. So we’ve set the forms action
to the URL as technical courses: your choice and choice dot ID, and the method we’ve used is post using the post
method is very important because the act of submitting
this form will alter the data on the server side, whenever you create a form that alters the data
on the server side, you will have to make use
of the post method and not the get method. So you all have noticed that I’ve made use of the
for Loop dot counter method. Now this actually indicates how many times the for tag
has gone through its Loop Since we are creating
a post form which will have effect
on modifying the data in our database. We need to worry about the cross
site request forgeries. Thankfully, you don’t have
to worry too hard because Django comes
with a very easy to use system for protecting against it
in short all post forms that are targeted at internal URLs should use
the csrf token template tag. So over here csrf actually stands for
cross-site request forgeries. Okay, so I hope
everyone’s clear till here. So the next thing that we’re going to be doing
over here is creating tests, which is very important for every website you create So
before moving on to show y’all how to write tests
for our website. I’m going to be sharing some
important details about testing. So first of all, what are automated tests tests
are simple routines that check the operation
of your code testing operates at different levels. Some tests might apply
to a tiny detail While others examine
the overall operation of the software. So that is something which is same as
what we’ve done while using our python shell. So we’ve use the python shell to
examine the behavior of a method and running the application
and entering data to check how it behaves. So what’s different
in an automated test is that the testing work is done
for you by the system. You create a set
of test once and then as you make changes to your app, you can check
that your code still works as originally intended without
having to perform time-consuming manual testing again, and again, so why do you
need to create tests? You may feel that you’ve done quite
enough on your plate? Just learning Python
and Django and having yet. Another thing to learn may seem overwhelming and perhaps
unnecessary after all as you all have seen
our website is working quite happily now going
through the trouble of creating automated tests is not going to make
it work any better if creating the technical
courses application is the last bit of Django programming you
will ever do then true. You don’t need to know
how to create automated test. But if that’s not the case, this is the best time
to learn testing. Okay, so tests can actually
save you time up to a certain point checking that it seems to work will be
satisfactory test in more sophisticated applications. You might have dozens of complex interactions between
components a change in any of these components could
have unexpected consequences on the applications
Behavior checking that it still seems
to work could mean running your coats functionality with 20 different variations of your test data
just to make Sure, you haven’t broken something which is definitely
not a good use of your time. That’s especially true. When automated tests could do
this for you in seconds. If something’s gone wrong tests
will also assist you in identifying the code that’s
causing the unexpected Behavior. Sometimes it may seem
a cord to tear yourself away from your productive
creative programming work to face the unglamorous
an done exciting business of writing tests particularly when you know, your code is working properly. However, the task of writing
tests is lot more fulfilling than spending hours of testing
your application manually or trying to identify
the cause of the newly introduced problems. Hold on tests. Don’t just identify
the problems. They also prevent them. It’s a mistake to think
of test merely as a negative aspect of development
without test the purpose or intended behavior
of the application might be rather opaque even
when it’s your own code. Will sometimes find
yourself poking around in it trying to find out what exactly it is doing
tests actually change that they light up your code from the inside and
when something goes wrong, they focus the light on the part
that goes wrong even if you hadn’t even realized
it had gone wrong test also make your code more attractive. You might have created
a brilliant piece of software, but you will find that many other developers
will simply refuse to look at it because it lacks
tests without tests. They won’t trust it. According to Jacob Kaplan Moss. One of Django’s
original developers code without tests is broken by Design other developers want
to see tests in your software before they take it seriously
and that is just another reason for you to write your tests. Last but not the least test
help teams work together. The previous points are written
from the point of view of a single developer maintaining an application
complex application will be maintained by
teams tests guarantee that colleagues don’t
inadvertently break your code. And also you don’t break there’s without knowing so now
moving on I’ll be sharing with you all a few
basic testing strategies. There are many ways to approach
writing test some programmers follow a discipline
called test-driven development. They actually write their tests before they write
their code this might seem counterintuitive. But in fact, it’s similar to what most people
will often do anyway, they describe a problem then
create some code to solve it test-driven development
simply formalizes the problem in the python test case
more often a newcomer to testing will create
some code and later decide that it should have
some tests perhaps it would have been better
to write some tests. You but it’s never too
late to get started. Sometimes it’s difficult
to figure out where to get started
while writing test. If you’ve written
several thousand lines of python code choosing something to test might
not be easy in such a case. It’s fruitful to write
your first test the next time you make a change either
when you add a new feature or fix a bug. Okay, so now I’ll be moving
on to show y’all how to write the first test
for your application. So the first thing that we’ll have to do is
identify the bug that’s present in our application. I’m sure many of you
wouldn’t have noticed but fortunately there’s
a little bug in my application as I’ve shown you
earlier the method that I’ve created which was was published
recently returns true if the course was published
within the last day but also if the all courses started
from field is in the future, it will show through now
the show this to y’all I’ll just open up the command prompt
and I’ll open my shell again. So I’ll just clear the screen. And now I’ll get
back to my shell. And over here, I’ll import
the date-time module. And then from Django doodles. I’ll import. I’ll import the times on method. Now after this, I’ll have to import
the all courses and the details class present
in the technical courses app. So from technical courses. dot models import all courses, details I’m not giving
a space over here. Okay. I’ve just made
a spelling mistake of here. I’ll correct it
technical courses. Okay. So now I’ll be creating a new
object over here and I’ll name it as future course, so I’ll just say
future underscore course. And the date for the publishing
of this course will be 30 days from today. So what I’m going to do
is take all courses. And I’ll take my variable
which was started from. And within this I’ll just
store time zone dot now. Plus date time dot time Delta
and I’ll specify that as 30 days. So this means that the future course
will start after 30 days from the current date. So for this I’ll use the was published recently
method and see what it returns. So I’ll say future course. Future underscore course is
the name of my variable dot was underscore published
underscore recently and now I hit enter so as you can see over here, the course is intended
to be published after 30 days. But when I use the was published recently
method it’s returning true, which is actually wrong. Now. I actually want to create a test
that will expose the bug what we have just done in
the Shell is to test the problem and it’s exactly what we can do
in the automated tests as well. So let’s turn this
into an automated test a conventional place
for an applications test is in the applications test dot py file the testing system
will automatically find tests in any file whose name
begins with test. Okay. So to create a test, I’ll open this test or py file
and as you can see over here, it has already imported
the test case method for us from Django DOT test class. So one more thing I want
to import over here is the data. Time module so I’ll say
import date time. And also I want to import
the time zone method present in Django dot utils. So from Django dot utils
import time zone. After this, I’ll also
have to import the models that are present
within this app. So for that Ali was from dot models
import all courses, so we here I’ll create
a new class so class. And I’ll just give
some random name to this. So I’ll say all courses. model test you can give any name
of your choice guys. Okay, so I’ll have to use
the test case method over here. So that’s test case within this
I’ll Define a function that is going to return
false for the courses who’s publishing date is in future. So to do that. I’ll just take a new
variable say time. equals x own judge now
and I’ll add this to date time. Time Delta. after 30 days Sorry guys without
specifying the function itself. I’ve started writing the code. Define I’ll just give the name as test underscore
was published. Recently, which means
it’s the test for was published recently
method with future course. And I’ll just hit tab over here. Okay, so now I’ll
take another variable which will be future question. And this will be equal
to all courses and started from Started from will be equal
to the current time plus 30 days now lose
the self parameter. And I’ll use
the assert is function. And I’ll use future
future question dot was published recently
and I’ll set this to false. So here we’ve actually created a Django DOT test DOT test
case subclass with a method that creates an all courses
instance with a started from in the future. I’m then going
to check the output of was published recently
which ought to be false. Okay. So I’ll just save this. I’ll open up the terminal
I’ll return the control back to my terminal by hitting
Ctrl C and over here. I’ll type in the command python managed py test followed by
the name of my application, which is technical courses. So what exactly
happened over here is this the managed of py
test command actually looked for the all courses application after that it found a subclass
of the Django DOT test the test case class it then
created a special database for the purpose
of testing then looked for the test methods the ones
whose name begins with the test in the function test
was published recently with future cores. It created an
all courses instance who’s start date is
30 days in the future. And then using
the asset is Method. It discovered that the
was published recently returns true though. We wanted it to return
false the test informs us which tests failed and even the line on which
the failure occurred. So as you can see over here, I have a message saying
the test has been failed. So the next thing is to fix
the bug we already know that the problem is
in all courses was published recently method and that
should actually return false if the publishing date, Of that course is in the future. So now what I’m going to do
is get back to my models that py file and over here. I’ll have to make changes to the
was published recently method. What I’m going to do is
add a new variable thin now and I’ll equate
this two time zones now. After this, I’ll just change
this return statement. So I just returned now – the dates time
dot time Delta. of one day and then I’ll compare
this with the published date. So now that I’ve made
the required changes, I’ll just save this
and rerun the test again. So it’s python managed
of py test technical courses. So as you can see over here,
unlike the previous result. I’ve got an okay, which means I’ve
actually fixed the bug. So basically what I’ve done
over here is identify the bug and I wrote the code that exposes it and corrected
the bug in the code. So my test passes generally
many other things might go wrong with our application
in the future, but we can make sure that we don’t inadvertently
reintroduce this bug because simply running this test
will one has immediately so that was just
writing a basic test. I think I’d prefer
to write some more tests for my application. So while I’m here I’m going
to further pin down the was published recently method because it’s going
to be very embarrassing if in fixing one bug I
would have introduced another so I’m going to add
two more test methods to the same class to test
the behavior of the method more comprehensively. So I’ll just open up
the test dot py file. I’ll Define two more
functions over here. So this will be test
was published recently. with old course So
this method basically returns false for the courses who’s published date
is older than one day. So I’ll just take
a new variable of here. I’ll say test and I’ll say
that this is time zone dot now. – date-time dot time Delta
to know more about time Delta. You can actually refer
to the video and the blog on editor Acres channel
for the daytime module and within this I’ll specify
one day and one second. I’ll create a new
variable say old course. And then I’ll use
the all courses class and I’ll use started
from started from variable and I’ll equate this to time. Okay, I’m sorry. I actually intended
to name this time not test. Now finally, I’ll be using
the asset is function and old course. . Was published recently
and I’ll set this to false now will be
defining another function. I’ll just copy this. And I’ll change it
from old to recent. I’ll just copy everything up from here and I’ll
paste it over here. I’ll make the required
changes so over here it’s going to be ours. And that will be
23 hours 59 minutes. and 59 seconds Which means
it’s less than one day and the value over here
is going to be true not false. So basically what’s going
to happen over here is if the course has been published
within a day it will return true and if it’s older than it will return false and
if it’s in the future also, it’s going to return false. Okay, so I hope
everyone’s understood this. So basically we have created
three tests for all courses dot was published recently method and this will definitely
return sensible values for past recent
and future courses. You can also create
a test for review. The technical
courses application is fairly undiscriminating. It will publish any course including the ones whose
published field is in future. We should actually
improve this setting the started from variable
in the future should mean that the course is published
at the moment but invisible until then so to create
a test for a view when we fix the bug earlier, we wrote the test first
and then the code to fix it. Fact that was a simple example
of test-driven development, but it doesn’t really matter
in which order you do the work in our first test. We focused closely on the internal behavior
of the code for this test. We want to check Its Behavior as it would be experienced by
a user through a web browser before we try to fix anything. Let’s have a look at the tools at our disposal
the Django test client Django provides a test client
to simulate a user interacting with the code at the view level. We can use this
in the tests dot p y or even in the Shell. So first, I’m going to start
with the shell again here. I actually need to do
a couple of things that won’t be necessary
in the test start py file. So for that I’ll just open
up the command prompt. And over here, I’ll say from Django
dot tests Dot utils. import setup test environment So now I’ll call the setup
test environment method. And I’ll hit enter the setup
test environment method installs a template renderer which will allow us to examine
some additional attributes on responses such as
response dot context that otherwise
wouldn’t be available. Please make a note that this method does not set
up a test database. So next I’ll need to import
the test client class from the Django DOT test module. So from Django DOT
test import client now, I’ll create a variable named
client and I’ll say this is equal to the client method. Okay. So with this we can ask our
client to do some work for us. I’ll take a new variable
say response and I’ll say this is equal to client dot get and I’ll just check
the response dot status code. So as you can see over here, it has returned 200
as the status code for this website. Now. This actually means that our website
is responding well now in case I want to retrieve
the content on this website, I’ll just use response dot
content and I’ll hit enter. So as you can see over here, it has returned all the content
that’s present on my website. Okay, so till now we’ve actually
been working mostly on the back end of our project. But if you’re thinking
the Django only helps you develop the back end
and not the front end wait Django allows you to make
your website’s beautiful as well now to do that, you will need to learn some of the HTML Basics
which I’ll be teaching you all. Now, I know many
of y’all would be aware of it. But for those who don’t know
I’m going to help you out by brushing the basics
of HTML to begin with let’s first describe. What exactly is HTML HTML stands
for hypertext markup language a markup language is
a computer language that is used to apply layout and formatting conventions
to the Next document markup languages makes the text
more interactive and dynamic. So basically text
can be converted into images tables links Etc. HTML is the standard markup
language for creating web pages and web applications. It’s used to describe
the structure of web pages using markup the HTML structure
HTML tags have two main types. That’s Block Level
and the inline tags Block Level elements take
up the full available space and always start a new line
in the document. For example, the tags, including the headings and
the paragraphs inline elements only take up as much space as they need and do not start
in the new line on the page. They usually serve
the format the inner contents of Block Level elements. Some of the examples
of inline tags are links and emphasized strings. The three Block Level tags that you will need
for an HTML document are the HTML tag the head tag
and the body tag. The HTML tag is
the highest level element tag that encloses every HTML page
the head tag holds meta information such as
the page title and the cassette finally the body tag and closes
all the content that appears on the page. So how exactly does html
work HTML documents and with the dot HTML
or dot HTML extension. You can view it using
the web browser. The browser reads the HTML file
and renders the content for the users to view it
each HTML page consists of a set of tags or Elements which are known as the building
blocks of web pages. They create a hierarchy that structures the content into
sections paragraphs headings and other content blocks. So no talking about the HTML headings
HTML headings are defined within the H1 to H6 tags. H1 defines the most
important headings. Whereas H6 defines. The least important
ones paragraphs in HTML are defined using
the paragraph tag or the P tag. There is no limit to the number
of paragraph tags. You can add to your content
links HTML links are hyperlinks. You can click on a link
and redirect to another document or a web page to create a link. You’ll have to make use
of the anchor tag or the are tagged. Okay. So images images
are required to beautify and depict complex Concepts in simple ways on your webpage
HTML images are defined with the IMG tags the source
file alternative text width and height are provided as
attributes to the image tag. The button element is used for creating an HTML button all
the text between the opening and the closing tags appears
as a text on the button. It defines. Clickable button inside
the button element. You can add text or images lists HTML provides three ways to specify
lists of information all lists must contain
one or more list element. So basically you can have
unordered list ordered lists or a definition list, which arranges your items
in the same way as they are arranged
in a dictionary. You can also create tables
in HTML using the table tag Rose in HTML are defined within the TR tag headers
are defined within the th tag and the cells are defined
within the TD tags. So now just to show
y’all one example of this. I’ll create a new
project of here. I don’t want to mess
with the current projects. I’ll just create
a new projects HTML. Okay, and over here, I’m going to show y’all
some basics of HTML. I’ll create a new file of here
that we an HTML file. I’ll just name it as sample. So as you can see over here, my ID has already provided
the basic HTML tags for me. I just clear this off from here and I’ll use some already
written in HTML code. So as you can see over here, I basically have
a title with says what is HTML after that. I just specified
the background color to be black and the font color to be white. I’ve written some text over here
saying welcome to Eddie Rekha and learn about HTML5 center-aligned an image using
the center tag and the image tag with Source alt
and the style of it. After that. I’ve created an unordered list which contains a few courses
that are available at Ed Eureka. Finally. I’ve created a button, but I’ve not linked
this to anything. So now let me just show you all
how this runs on the browser. Okay. So here it is. I have a black background. I have white font color. I have an image and I
also have created a button which have not
linked to anything. So now let’s get back
to our project. So over here to design
my courses dot HTML page. I’ve already created
some HTML I’ll just copy paste that code over here. And I’ll save this and I’ll
rerun my development server. Okay, I forgotten
to run my project. So I’ll use Python
managed to hpy. runs over Okay, so now my server
is ready to run. I’ll reload this page. So here is the web page
that I have created. I’ve created a nav bar
and over here. I have a few options and
when I click on these options, it leads me to that particular
page on the Adriatica website. Okay. So when I click on edit Erica, I go to the official
audio record on your website. When I click on blogs and videos it will lead me
to that particular website of Eddie Rekha in case
you want your links to open in a new page. You can specify the target
element as underscore blank. Now when you do this, what happens is when you click
on the link it will open on the new page. Okay, so there when I
clicked on community, it has gone to the editor
a community website. So as you can see over here, I have all my options that is python Java and c
and I also have some description regarding the self-paced
and the instructor-led courses. I’ve also added
on some images to my website and at the end I’ve specified
a footer with some text regarding my website. So I hope you guys
have liked the sample website that I’ve created. However, there are
enormous more options that I leave you guys
to explore before moving on towards the do’s
and don’ts of Django. I’d like to take a quick recap
of what we have done till now. So is the first place we’ve seen how to install Python pycharm
and Django after that. We created a Django project
using the Django admin following that we’ve seen
the various files that Django created for us along with the project namely
the managed py settings dot p y in added py W SG. I dot p y and the URLs dot py
files And then using the manage to py file, we’ve got
a development server running. The next step was creating
a first app using python manage dot P white
startapp command after that. We’ve seen how to create
our models views and templates. I’ve also shown you guys
how to connect the project to database using migrate and
the make migrations commands. Also our project
has an admin section that has the ability
to make changes as in how the admin decides to do it. We’ve also created a few tests
for our website in order to keep it free from Bugs. Finally. We’ve seen how to beautify
our website using HTML. Okay. So I hope you guys
have enjoyed this demo something very important
for you guys is the do’s and don’ts of Django. So coming towards the do’s and don’ts while writing
templates views and models. So while writing
the Django template code but one and only one space
between the curly brackets and the tag content. So as you can see on the screen when you’re writing something
between the curly braces keep in mind not to
insert Extra Spaces and also avoid not giving
any space at all. So while writing the Django
views the first parameter in a view function
is called as request. So we do writing this parameter
specify it as the complete name which is reqest and not req or any other short
forms the models while writing your
models make sure that the field names
are in lower case and it’s better to use
underscores instead of camel case the order
of models inner classes and the standard methods
should be as follows. The first should be all
database Fields followed by the custom manager attributes then is the class meta followed by the Ring method
safe method get absolute URL and any other custom methods that are left over. So those were a few do’s
and don’ts of Django. So moving on towards
the most frequently asked questions about Django. The first one is according to Django the most
frequently Asked question is why does this project exists
Django actually grew from a very practical need World online a newspaper web
operation is responsible for building intensive web applications on
journalism deadlines in The fast-paced Newsroom World
online often has only a matter of hours to take
complicated web application from concept to public launch
at the same time. The World online web developers have consistently
been perfectionists when it comes
to following best practices of web development the World
online developers ditched PHP and began using python
to develop its website as they built into Civ
richly interactive sites such as Lawrence. Cam they began to extract a generic web development
framework that let them build web applications more
and more quickly. They tweak this framework
constantly adding improvements over two years in Summer 2005 World online decided to open source
the resulting software, which is Django Django
would not be possible without a whole host
of Open Source projects. That is Apache Python
and the postgre SQL. Okay. So next time on the list is
what does Django mean? And how do you pronounce
it Django is named after Django Reinhardt who’s actually a jazz guitarist from the 1930s
to early 1950s to this day. He’s considered one of the best
guitarists of all time. Django is pronounced as Jang. Oh, make sure to keep
the Diaz silent. Another very common
question is that if Django is stable? Yes, it’s quite stable companies
like discuss Instagram Pinterest and Mozilla have been using
Django for many years. Sites built on Jango
have weathered traffic spikes of over 50,000 hits per second. Okay. So now moving on many
people also asked if Django can scale. Yes compared to the development
time Hardware is cheap. And so Django is designed
to take advantage of as much Hardware as you can throw
on it Django uses a shared nothing architecture, which means you can add
Hardware at any level that is database servers caching
servers or web applications or servers the framework cleanly separate components
such as the database layer and the application layer and it ships with a simple yet
powerful cash framework. So now to answer the question if Django is
a Content management system. No Django is not a
Content management system or any sort of trunky product. It’s a web framework. It’s a programming tool
that lets you build websites. For example, it does not make
much sense to compare Django with something like Drupal. This is because
Django’s something that you use to create
things like Drupal, of course Django’s automatic. Admin site is fantastic and time-saving but
the admin site is one module of Django the framework. So what python version can you use with Django Django
can be used with any of the Python versions
starting from 2.7 till 3.7 Python 3 is
actually recommended now, this is because Django 1.1 is
the last version to support Python 2.7 since newer versions of python are often
faster have more features and are better supported
the latest version of Python 3 is recommended
So one of the very important and the most frequently
Asked question is how to make changes to models
and to update the database. Like I’ve already
told y’all before when you make changes
to the models just py file you have to run
the make migrations command, which is nothing
but python managed or py make migrations now, this will create the migration
for those changes after that. You’ll have to run
the migrate command which is nothing but python
managed to at py migrated to apply the changes
to the database. The reason that there are
separate commands to make and apply migrations is because you’ll commit migrations
to your version control system and ship them with your app. They not only make
your development easier. They’re also usable by other developers
in the production. So can Django support
nosql databases nosql databases are not officially supported
by Django itself. There are however a number
of side projects and forks, which allow nosql functionality
in Django like Anand Rel so how can you customize
the functionality of an admin interface? You’ve got several options? If you want to piggyback
on top of the add or change form the Django
automatically generates, you can attach arbitrary
JavaScript modules to the page via models class. Admin JS parameter
that parameter is a list of URLs as strings pointing
to JavaScript modules that will be included
within the admin from via a script tag. If you want more flexibility than simply tweaking
the auto-generated forms, feel free to write
custom views for the admin. The admin is powered by
Django itself and you can create custom views to hook into the authentication
system check permissions and do whatever you want. This session is mainly
a practical session. But before we begin
the Tactical aspect, there is some amount
of technical knowledge that you need to have so
we begin the session by first understanding Is an API and how @api
fits into the Django after that we’ll have
a brief introduction to this framework. And what exactly is this API
and how does it work? Then? We’ll move on to a demo part. We will be building
a restful web API from the scratch. So let’s begin the session and discuss the very
first topic that is what is an API so API refers to application
programming interface. Now, let me break it down
into simple words. So the very
first is application. Now, you must be aware
of an application. If you’re using
a smartphone say the games that you play in your phone
or the social networking apps that you scroll around or any other software that you
use in your day-to-day life. So all these comes
under your application. Next is your programming. So programming is basically
a set of instructions that you tell a computer
to perform a specific task Now using that program. You can build a software which
makes the life much easier. So that is what programming
is last is your interface now when interface is a point where two applications or you can see
programs meet at In graph with each other now it basically allows to communicate with one
another and hence API is a way for programmers to communicate
with various applications. Now if you go
by the Google detonation it is set up different subroutines different protocols as
well as different tools that you need to build
an application software. So these are the terminologies that the developer
must be knowing. Now. If you look at the image
to the right, you can see
how different environments that are running
in application are communicating with each other through the API
now for running these API, all you need is an environment
that supports the application and the API which is a part
of an application. So with this I guess you’ve got
a simple understanding of what is an API. Now if you have any doubts
any query related to any of the concept that I will be teaching you
can feel free to comment below and I’ll be happy to help
you with the same. Now. Let us move on to our next topic that is an introduction to rest
Frame Book today’s basically describes an architecture which stands for
representational State transfer it is used. Both this of their baby is
for data communication. It also supports some of the common HTTP methods
to make interaction between the machines
or you can say applications. Now some of the HTTP methods
that are commonly used in this architecture
are get put post and delete. So the very first method is dead now get is basically used
to return the records or you can see the data
that you filled in. So basically get is used
to retrieve a resource. Next is your put so
put is used to change the state or you can say update a resource
which can be a file. It can be object
or any block thirdly its post post is usually
used to create the resource and last is delete which is used to delete
or remove that resource. So this was all about
resp am work now. Let us see
what exactly is a rest API. So any web service that uses a rest architecture
they are called as display apis or you can see rest apis. So you must have heard
about Facebook apis or Google apis
or maybe Twitter apis. So all of them are –
hey, PA, similarly, if you take it on yourself
say for example, you own a website. You have all your code. You have your
database connection and everything is working fine
over there now suddenly you have more users coming
to your website and then you decided
to build an iPhone app, or maybe an Android app. So now your next stop would be how to connect
the database online. And now let’s say
some more requests come in and you want to make
a software desktop application. So again the
same question arises that how an iPhone connects or how an Android
connector database online. It needs a website data from there and update
the same over here. So isn’t this looks confusing
and extremely complicated? So what you can do here,
you can create a restful API which will serve all
your purpose in one go now making a restful API, you need to have a device access to web site data
through a common format. So here we’ll be using Json now. Json is the data format and it is not at all specific
to any of the device. So basically use
Json to communicate within a specific format so that we can build
an Android app and IOS app or any other software that you want to make now
one more key feature that comes into picture is that if you have an API that
performs certain operations, then you don’t have to rewrite
that as part of your code. And thereby it also reduces
the size of the code now, I guess enough
of theoretical part. So let’s just directly
jump into the demo where I’ll be helping
you to create the first restful API from scratch. So for that you need to First
install the rest framework, so everything can be installed
using python package manager. Now, if you’re
using command line, it is very easy to install as you just have to type in PIP
install Django rest framework, or if you’re using pycharm, you can directly install it
by going to your settings and it’s the same way you do it
for other packages now once your installation is done, you need to add this framework
in your installed application. So let me go back
to my project and explain you how practically it is done. So first of all,
let us create a project. I am in my C drive users. Ayush and my Chum project. So here I create
a new folder by calling it by Django rest framework. And inside this will
be creating a project. So let me go
to my command prompt and I will give this location. Now once you’re under this, let’s just first
create a project. So for creating a project you
have to type in Django admin start project for it by the name
of the project is in my case. The name of the project
would be my project. So as you can see here,
I have my project. So once you click on it,
it has one more file, which is named as my project
only and one manage file. So if you go and click
on my project, it has some of I’ll say
in its settings URLs and wsgi. So if you have any confusion, or if you want any explanation
rated these files you can go to my previous recording and get
the concepts here. Next what we have to do we
have to create an application. So for that lets
go to my pycharm and open this project. So this is my community
version of Pi jump. So let me open it. It’s in my see users and chunk projects tango lesson
over and my project it is. So this is my pie chart now, you can do the exact same things
over here as well. Now for that you can go
to the view tool windows and you can open up
the terminal like this you first have to install
your Django rest framework. So for that you have to type in pick install
Django rest framework and can click on enter. So now I’ve already installed
my Django rest framework. So it seems that requirement
is already satisfied. That is like mine. Well, just clear my screen. So now I’ll be creating a simple
application say web app. Now. What are we doing
in that application is I’ll be storing some employers
information say for example, your first-name lastname salary
and all that other stuff. So for that you first need to type in Python manage
dot P Vai startup and the name of your application. Let’s say it’s web app. You can do the same
in your command prompt as well. But I feel my charm is
more convenient for me. So if you go on my project, you see the web app
is initialized over here. Now inside this it has
different files it has in it. It has admin panel. It has application does models
and many more now if you remember I
have already explained in the presentation that first you need to go
to a project settings and configure everything. So I’ll go to my project. This is my settings and inside this I have to go
to my install applications and have to write
it rest framework. I also have to write
in the name of the application that have just created. So once my project
setting is configured to what I’ll do next I go
to my application and I’ll go to my models. So here I’ll be creating a model or you can say database
very good feeding all the necessary details
of the employed. So here I create my first model. I don’t need this so I will create a class
over here grasp employees. modest Let me fill in some
details for the employee class. Let’s say your first name
of the employee or the last name or some employee to it. So let me feed
in some details to it. But I’ll mention
the max length to say 10. Next is my last name attribute. Again, Max and should be
10 and let’s say one more field. I want to feed
in say employee ID. This should be
an integer field next what will have will have
a string representation of this. So here what I will be doing. I’m creating a method
that returns all your fields. And it returns say
self DOT first name. Next what can I do? I can go to my admin
dot py file now here I can add and delete employees
from the admin panel. So for that we need
to First import the model that you’ve just created so
here my Adam is important. So here I have
important my model which is named as employees. Next up is register this model so that we need to write
admin dot sight register and your name of your mother. That is my employees. So once your admin
panel is created, you need to First
create a super user so you can again go to your terminal and you can
create a super user for that. So here is my terminal
I can directly write python manage dot P
by P it super user. Oops, no hate you some error or because I’m not
migrating my table. So first we need
to migrate a table. So for that we need to type
in Python manage dot P Vai and make my gracious. So this will basically
update your table structure. So once you migrations are done,
you just need to migrate it. So you have to write my great. So basically creates your table
with the current structure and it feeds in all the details that you have written
in your model. That is your employee class. So here your project
has been migrated now. Let me create a super user. So notice me for username. So let me type
in my Yoshi email address. You can leave it blank as well
and after given some password. Make sure you have
a strong password for this. So now my Superior created
successfully so now I can turn my server for unmask
over the command. Is python managed
dot P by 1 Circle? Now it is successfully started. So I’ll open my Google and I
write it over here localhost. So here as you can see, there is no web page
that can be displayed over here. But we do have
a sweet admin page. Let me open the add new
page view so ask them for the username and password
that are just created. So my username is IU she
and I have some password. So as you can see here, I have that web app that
I’ve already created inside that I have graded and model
which says employees now once we go into that it has
an option to add an employee. So now let me add some details
to these employees. So the very first name
is say Swati last name. Let’s an employee ID is 1 now. Let me add some more to this
so it may be baked. Say to and I’ll add
one more appeal Ash. So the employee ID, so and I’ll add one
more that is so neat, but I do spy. Now here as you can see
there is a list of employees that have just added now. Once you click on them, you’ll see all the necessary
information regarding it the first name the last name
and the employee ID that you have assigned to it
and you can directly delete it from here as well. Now while going back to it the seat again the list
of the employees now, let me go back
to my code and see what else you have to do. Now. Let us create a CLI is a class. Now. This class is used to convert
your model to your Json data. So Jason and I have already
told you this in is a format and is know-it-all specific
to any application. Now Jason is used because whenever a user
requests a web site, we usually send them back a regular old HTML format
which team responds now, however, we don’t want HTML. It’s only when you want
information from the browser. So here we’ll be sending
them Json for that. We need a way to convert
this model to Json. So hence as Eliezer is used
so I’ll go to my application. And I create a new
python file and name it as she realizes. So you can see this file right
here in the web application. So inside this we need to import
some stuff from this paper. So first I’ll import
those things from rest framework Imports eliezer’s
Next I have to import my model that is employees. So here now. Let’s create a class. So class employee
forward bias utilizar. teenagers So here
this is my model name, which is my employees
followed by a Serial iser. So whatever model you want to
see the lies just say the name of the class followed
by his Eliezer keyboard next this utilizers not model serializer is your inbuilt thing you
don’t have to worry about this because you just have
your modern as a blueprint which the serializer needs
to convert to Json format. So next what we have
to do we have to create a class meta and for
the configure it so I’ll create a class
and fill in the various fields that are used
in an employee model. I’ll create a cast metal and here I will write
model the name of my model is employees next have
to configure my feel. So if I open my models and here I have first name
last name and employee ID. So let’s say I just need
first name as a response. So here what I’ll do
I’ll hear right feels equal to your first name. If you need one more field, let’s say last name you need
first name you need last name, but you know need employee ID
so you can manually write that you need to display only
first name and last name or if not this you can do one more
thing to display all your fees that is feels equal to underscore underscore all
and double underscore again. So this is basically
return oil fields present in your employ a model. So now it will display a first
name last name and employee. So I have done this because I don’t have
much feels to return that’s why I returned all next. Let us go to the Views
to py file and see what we need to display
when we hit the API. So this is my view
top view by file. So in this file top you
have to do we have to just request an API and get
the Json back for that. First of all,
you need some imports. So I’ve already copied them. So let me just paste it over
here the say it’s my time. Now don’t worry. I’ll explain each one of them. So the very first is by default. Next is your HTTP response, which have been using
for any program to turn. The response. Next is your get object or 404. Now we get 404 window
object doesn’t exist. Say for example, York where some random
name let’s say Emit and this doesn’t exist
in the company’s database. So what will happen
it will return a photo for so for example, I show you practically so
as you can see here, I have these men employs over
here and say I have requested some different name. Let’s say I’m it and this doesn’t exist
in my company’s database. Therefore it will return a photo
for now coming back to my code. Next is your API view
Now API view is used so that the normal view scan
return an API data next is your response. So this is where you
get back the status or a particular response. Now, everything went fine. It will deter a 200 response or you can see two zero zero
response similarly the 404 that we have just discussed. It says that you
have requested something but it doesn’t exist anymore. So these are some
of the HTTP responses and there are many more
like that say 401 is not found 500 is a server error
and things like that. Next is your status which will basically
send back status. After that. We have the name of the model
that is my employees and then the name of the serialize or
that is employee serializer next what we have to do we have
to create a class based view which basically inherits
from an API view. So let me create a class
for it C-Class employee list. And this inherits
from an API view. So here we will create
two method get method and a post method. So the very first method
that is your get method. It is used to return all
the employees in our model and then the second method which is your post method
it helps you to create a new employee. So let me create
these methods one by one. And the next method
that is my post. So now what I’ll do is I’ll go
in my first method. I’ll just remove the scars
which basically means to nothing and here I will pass
in the request. Now we’ll create a variable
which stores all my object. So let me name it as employees or you can see employees
one will have my model and will say object at all. Next we have to see you
like them which means that it will take
all your object and convert them into Json. So I’ll take this
here Eliezer for that. That takes Eliezer. So this is the name
of my serializer and here I will pass
in the employee which means all your objects and the second parameter
would be many is equals to 2. So this second parameter
that you can see here. It means that there
are many of them. So here you don’t have to return
just one Json object next. We know every view function
returns an HTTP response. So in our case, it’s Json. So what we have to do we
have to return a Json. serialized data
This is sterilizer. Next is imposed function with
say submitting all your data. Whereas your gate function
is reading or taking your data. Now with this we have
completed our views pain. Now what you have to do
have to connect or link it. So for that we have
to go to URS fine. So here we go to a main project that is my project and we’ll go
to URL start py file. So here you have
to add some imports which are compatible with apis
and rest framework. So I will add here
from this pain book. Explaining later as we go on and have more functions
to play with. So in my code, you will not be requiring this
as such but other things you require is to import your view. So for importing
your view you have to type in import loose now, as you can see here, it is only
linking to my admin panel, but now we have to create
our own URL to display the view for that. I’ll just copy the same line
and I’ll paste over here. So you must be knowing that these are in the form
of regular expression. So in spite of admin I can type in employees so that
whenever I hit on employees if you return my view just to make it easier I have
to type induce dot employee list that we have just created. So by hitting on employees, it will connect to my view
and in my view there is a class called as employees. So I’ll go to My Views
and here I’ll see I have my list that is employee list and we’ll turn these things
as a view therefore going on employees. It will go to My Views
and then it will turn the class that is employee list as view. So in this way, we will get the data
in a Json format. So with this
Accord is completed. Now what you have to do you
just have to throw this URL and connect to
any website database and from that you can get all
the information from any device in the world be it. I phone with Android anything. So now let me go back
to my server and see whether this API
is working or not. So this is my Local Host till I have to write
employees which is My URL that I’ve just created. Okay. So this is showing me an error
that name true is not defined. So let me go back
to my code and see in My Views file. Now this will work. So well again, go to my local host
and I’ll refresh it. So, yes, it displayed. So as you can see here, I have
all my employee information. I have their IDs
their first name and the last name. So this is the format of Json which are basically
surrounded by curly braces having all your data
and a descriptive title to it. Now using this URL. You can check it anywhere and
retrieve the same information. You can get it using any device in the world with your iPhone
Android or anything. So I’ll just copy this URL
and go to the browser and I’ll open my Postman. So here I just paste my URL. So as you can see here, we are different
types of requests. We have Gerd we have post put
patch delete and many more. So here I’m going to Simply
make a get request. So I already pasted my API
and does I have to send it. So yes, this data
is going displaying. So as you can see here,
I have my data in Json format, which means that my API
is working perfectly fine. Let us move forward
with the project. So here I have just
created a demi website which displays all your
courses the description along with the date and time bagged up
with the database. So in this I have used
few of the companies that we have discussed
and some of the advanced stuff. So let me just show
case the project view. So let me open my project. It’s in my DIY Yoshi and Django
project Django tutorial. Even current window now, let me just run
the server and let’s see what exactly this place. Oops, I guess I
have not imported with the manager py file. So let me do it again. So inside Django toil demo. I have a managed a py file. So again, I have
to run my server so we serve is now running. So let me just open it
and I’ll just go to Local Host. So this is how my website
looks like now here. I have some static files
and some text displayed. So here is
my python Java tutorial, which is the header
of the website and here in the left hand side. I have their do
regalo go just below that I have a navigation bar
which contain three tabs. That is Home tab courses Tab and contacts tab after that
in the same div container. I have place for static files, which says Eddie Rekha
has live classes. It has exposed instruction. It has wrinkles and support
and flexible scheduling and towards the end
in the photo. I have given contact you
can contact the Ada regard supported recorded for then we have
Facebook Facebook page of a dairy cow opens up next. We have YouTube. So here I have given the link which opens up
the Eddie reoccur channel. So this is
how the website looks like now if I go to home it
will just Okay, because I’m doing it Rica but if I go to courses so it will provide
a lot of courses. So here I put a slider with changes automatically
with a number of objects that it has. So right now I just
have Django and python. So if you click on python, the detailed description
will be displayed. So Python and The Core
subscription is below and when it was published,
it’s all written. It’s the date and time
has been mentioned similarly. If I go to courses again same
is the case with Django Django, it displays the description
and the date and time with respect to it
last we have contact. So in contact I have just
displayed The View which says contact in support and Erica.com which basically
gives 24/7 support. So this is how my website looks
like now in the courses tab, I just have two objects. So let me go to admin page and
let’s add few more courses. It’s already created
a user name for this project. So the username is IU she
and the password and specifying so this is
my Administration panel that we have just split now one more thing that you
must be noticing over here is the courses now here you have just let
me pull it through here. So inside courses I have
a model which is post. So once you click on this it
has boast now here it displays just two objects now here I
can add unlimited courses. Now, let’s have
to upload a new course which has a name. Let’s say up
here and description. I just write this
is a new course. So then you can just provide
the date you can select today and you can select
the time is now and then you just
simply have to save it. So now as you can see
the post has been added once you go back
to your website here will see the course will be added. I’ll just refresh this page. So here as you can see our PA
is added now if I click on that it will display with the description
the date and time. So this is
how the website works. So now let me just walk you
through the code of it. So for that I’ll just
come out of my server. So this is my project structure. Now, the main application
that I have is demo. So I’ll go to my project demo and I will see the settings are
you can see the URLs where the user is been hitting. So there are three URLs. The first is the admin
which is by default. Secondly, I have created
my daughter URLs and then I have created
a separate application which is courses and I’ve given a separate URL
that is courses that URL. So these are
the two applications that have created and I’ve given
a separate URL to it. So now let me go
to my first application. That is my so I’ll go
to my application and go to URL State p y so here
I’ve defined my URL patterns. Now, we know that you are
a patterns are in the form of Rebel Expressions. So this is my home page
where we have a view where we have index method which must be placed in View and we have given
a name space to it second. We have contacted again. I have created contact method
and given a name space to it. Let me just review
these two methods in the views of py file. So here these two
methods are defined. So the very first
method is index. So here we have again
not return the HTTP response. So here what we have done. We have simply
return the templates. So what I have done I have I
have created a template which is home dot HTML and there
I have passed dictionary which has a key post and then it has post one
and postal similarly contacts. I have created one more HTML
that is basically HTML and there I have just
passed a dictionary which has a kiosk content. So this is my dictionary and then I have passed
the content to it, which says contact supported
Eddie regarded go. So, these are my two temples which says home but HTML
and basic dot HTM with so let me just go to my
template folders and let’s see what exactly is
their home in basic. So as you can see here,
it was extends headed on HTML and then it will just
display this text. That is he Welcome to edu Rica. So this is present
in the ginger logic. Now, if I go to header dot HTML, which it has extracted,
so let me just go to Hello dot HTML
and here you’re all HTML is present. So this is my header part where I just displayed
will come to order a car and then I have load
some static files which are the images now inside that I’ve also
used some bootstrap which has a link of this. So if you come down
you have a body class which has a div container. So if you guys are
familiar with HTML, so you will really
catch up the soon. So inside this the face
of a static file and the text accordingly. So here I have given
the source of the image. I paid one more folder that is static
and inside static. I have my application. Then you have folder
named as image and then the name of the image. So if you see here, I have a folder
which is static then it has the same name as that
of my application. And inside this I
have already told you that I’ve used some CSS. So this is the bootstrap
that I’ve already downloaded. So it’s up to you if you want to use it
or you don’t want to use it. So just to
represent this better. I have just downloaded the bootstrap and then
I’ve just put it over here. So I will just show
you from where you can download the bootstrap. So I’ll go to Google
and I’ll just type in bootstrap. So this is the website from where you can
download a bootstrap. So here it has a lot of things that you can tuck this so here
it says download bootstrap, you know the source
and many other things are there. Now if you see
at examples you can find a million examples over here. So what I’ve done
I’ve just simply downloaded this bootstrap. So this is a ZIP file
that comes with us. So what I’ve done I’ve just
extract this folder and just paste it over here. So this file contains
a CSS your phones and your JavaScript. So this already contains a lot
of things it has CSS. It has themes
and many other things. Now. The one thing that I have added
is the image folder now here if you see I have added
these six images in the website if you click on that. So this is the first image
that I’ve added to the website. Let me show you where this image is
exactly on the website. So this is the first image
which has a name of Life gases. So this is my first image
similarly there is second image. And similarly I have
other feature images as well. This is my feature to this
is my picture 3 and this is my feature for so these
are the all the static files that I’ve uploaded
to this website. So here in my div tag, I have given the source
as static my image and the name of my image. Similarly. I have done it for
all the images next. I have created a navigation bar, which is there
in the left hand side. So this is my navigation bar which contains three tabs that
is home courses and contacts. So this is my home
courses and contacts. So here I have used the list which goes to different URLs
at the same time. So if I go to my home, I have a different view
which is displayed if I go to courses, I have different View
and similarly with contact. So this is all about my left
navigation bar again. I have created one more div tag where I have placed
these static files. Snakes and also created
one more div tag where I have placed slider. So this is a slider
that I’m talking about. This is this code
for slider and in the end. I just added a photo so input or I have just added
the hyperlinks to it. So if you click on contact
a new URL will open if you click on Facebook
a new link will open and similarly with the YouTube. So my my application
is completed now, let me go to the other
application which is courses. So if you go to the URLs
of the main project this I have already covered so next let
us go to the courses application and just hop into courses that URLs.com so
courses I’ll go to URL stir py file. So here I’ve created
a URL pattern now here. I’ve already told you
that in courses. I have created a list. So let’s it collects
different objects, which is created
in a admin panel. So here I have written
a query state which says post which is the name of my model
and then we have set. All the object and then we
have ordered by date now – it represents
the descending order and if you want
to place a limit to it, you can place a limit. Let’s say 25. So only 25 courses
can come at one go next. I paid one more template
which is courses of HTML and if you need a detailed
description to it, you have to create another URL which has a PK
which is a primary key. Now, we will not create a primary key Django
does it for you? So here it will represent
the first column in the module. So here I have specify
the models my post and here I have displayed
one more template which is inside my courses
and excess post dot HTML. So let me go to courses
and post at HTM. So inside my templates, I have two templates
that is courses and postage label. So this is the HTML
for my slider. So it’s again
extending my main HTML, which is header dot HTML
The Inheritance is taking place by the keyword that is extinct. Now everything is
inside your Ginger logic now here I have started a loop
with says put Post-it List, the object list is the list
of all the objects which are there in post now. What is post
supposed is my model. Let me first go to my models. So here I’ve paid one model now, I have created a class
which is post. I have different feels like
course description and date. So this course has
the maximum 140 description. It has a text. We love this
and similarly the date or create one more method which basically returns
your cell dot course. So once your model
has been created, let me go back to my courses
at HTML and he replied for Loop which says each object
should come in the list. So here I have displayed
the post date date should come in this format to display
the year first then the month followed by the date after that. I’ve just given a hyperlink which says courses
and it will fetch the post ID, which is the work
of primary key. Basically, it will just
tells which column or which forces it has picked up
from so this is my slider. Next is a poster HTML. So in this post dot HTML, I Just inherit the same
header or HTML file. And inside this
I have to display the course description and date. Now if you notice here, I have written post
at body safe and line breaks. So if you go to my website
and if I go to inspect so you can actually see this
in different formats. Let’s see if you want to see
this website in iPhone 6. So this is how my website will look
like an iPhone 6 similarly. You can try for different
devices as well. Also if I close
these tabs Now similarly if I open my tab and
if I squeeze this window, it will not change anything. So this is what these things do. So this was all
about my project. So I hope you get
a simple understanding of what exactly all these components and how exactly
these components work. So now moving on towards
the final topic of this session in which I’m going to be sharing a few very important
Django interview questions. Now one of the most
common question of Django is if it is better
than flask or not Django and flask both map the URLs or addresses typed
in the web browsers to functions in Python flask is
much simpler compared to Django but flask does not do
a lot for you. Meaning you will need
to specify the details. Whereas Django does a lot for
you wherein you would not need to do much work Django consists
of pre-written code, which the user
will need to analyze whereas flasks gives the user
to create their own code. So now we’ll be going
towards the next question which is the difference
between Django pyramid and flask flask is actually
a micro framework guys, which is primarily built for small applications with
simpler requirements in flask. You have to use
external libraries. However, flask is ready
to use pyramid is built for larger applications. It provides flexibility and lets the developer
use the right tools for their product the developer can choose the database URL
structure templating style and more pyramid is
highly configurable Django on the other hand can also
be used for larger applications just like pyramid and it also includes an orm orm actually stands for object
relational mapping layer. Now, this layer can be used to
interact with application data from various relational database
such as sqlite MySQL Etc. So now moving on
towards the next question, which is the
Django architecture. Like I’ve already told y’all before Django follows an MVC
MBT architecture MVC stands for View controller it is used
for developing web applications where we break the code
into various segments here. We have three segments namely the module View
and the controller module is used for storing
and maintaining data. It is the back end where your database
is defined views on the other hand are
all about presentation and it is not at all aware
of the back end it basically deals with more of HTML
a controller is a business logic which will interact
with the model and the view. So now talking about the MBT architecture
of Django mvt stands for model view template in mvt. There is a predefined template
for user interface. Say for example, you write several
static HTML forms, like hello user 1 hello
use with 2 and so on the template you
will be having only one file that prints hello along
with the variable name. Now this variable
will be substituted in that particular template
using some ginger logic. That’s the magic of template. You don’t need to rewrite
the code again. And again, however in the MVC architecture
Django itself takes care of The controller part
so now moving on towards the next question which is how to set up
the database in Django. So I like I’ve already told you
all before the sqlite database comes as a default configuration
along with Django if you’re new to databases and you’re just interested
in trying Django, this is the easiest choice
sqlite is included in Python. So you don’t need
to install anything else to support your database in case you’re working
on some real big project. You will need to use
a more robust database like Oracle MySQL Etc
now to connect a project to the database you’ll have
to type the command python manage not py migrate
the migrate command. Like I’ve already told you all before we’ll look
at the installed apps settings and creates any necessary
database tables according to the database settings
in your settings dot py file. So now moving on
towards the next question, which is given example of
how you can write a view in Django for an example of
how to write a view in Django. You can actually
refer to the code. I’ve shown you
all before so here. What our final view looked like
a view basically shows the user what he sees on the website and like I’ve already
explained all this to y’all before it either has a response
or a random method which will return the required
data to the user on the website. So here is an example
of you guys. So now let’s move on
towards the next question, which is mention what the Django templates
consists of the template is a simple text file. It can create any text-based
format such as XML CSV HTML Etc a template contains variables that get replaced
with the values when the template
is evaluated and the tax that control the logic
of the template. So now moving on
towards the next question, which is what is
the use of session in Django framework Django
provides a session that lets you store and retrieve
on a per site visitor basis Django abstracts
the process of sending and receiving cookies
by placing a session ID cookie on the client side and storing all the related data
on the server side, so The data itself is not stored
on the client side. This is actually very nice
from a security perspective. So now moving on
towards the next question, which is list out. The inheritance tiles
in Django in Django. There are basically
three possible inheritance tiles the abstract base classes. This style is used when you only want the parent
class to hold information that you do not want to type out for each child model
The multi-table Inheritance The multi-table Inheritance
is used if you are subclasses and existing model and need each model to have
its own database table. The third one is proxy models. You can use this model if you want to modify the python
level behavior of the model, however without changing
the models fields, So now moving on what is
the difference between a project and a nap now many
of us actually get confused between apps and projects
an app is a web application that does something for example in the web
block system a database of public records is
a simple app a project is actually a collection
of configurations and apps for a particular
website a project can contain multiple apps and app can be
in multiple projects. So now moving on
towards the final question of this session, which is what is
a Model A model is actually a single definitive source
of truth about your data. It contains the essential fields
and behaviors of the data. You’re storing Django
follows the dry principle or the do not repeat
yourself principle. The goal is to Define
your data model in one place and automatically derive
things from it. This includes the migrations, for example migrations
are entirely derived from your models file and are
essentially just a Treat the Django can roll through to update
your database schema to match your current models. So with this we’ve reached
the end of the session on Jango full course, make sure to practice as much
as possible and in case you have any doubts or queries, please do let me know
in the comment section and I will revert to you
at the earliest good bye and take care. I hope you have enjoyed
listening to this video. Please be kind enough to like it and you can comment any
of your doubts and queries and we will reply them at the earliest do look out
for more videos in our playlist And subscribe to Eddie
Rica channel to learn more. Happy learning.

10 Comments

  1. Got a question on the topic? Please share it in the comment section below and our experts will answer it for you. For Edureka Python Django Training Curriculum, Visit our Website: http://bit.ly/2npGIKi Use code "YOUTUBE20" to get Flat 20% Off on this training. Video Timestamp:
    2:11 Why Python is used for Web Development?

    3:36 What is Django?

    3:06 Python & Django

    3:11 Django Features

    3:56 Django Architecture

    5:36 Prerequisite

    6:01 Django Installation for PyCharm

    1:36:47 Writing a test

    1:57:07 HTML Structure

    1:58:02 How Does HTML work?

    1:58:37 HTML Structure

    2:04:22 Do's & Don'ts

    2:10:42 Django REST Framework

    2:11:12 What is an API?

    2:12:47 Introduction to REST API

    2:13:42 What is REST API?

    2:15:47 Django Rest Framework Installation
    2:32:52 How to Develop a Website From Scratch?

    2:45:47 Django Interview Questions

  2. Great 💐💐💐 Explanation is Good
    More videos upload django topic …
    Special video upload interview questions..

  3. :: TIMESTAMPS ::
    2:11 Why Python is used for Web Development?
    3:36 What is Django?

    3:06 Python & Django
    .
    3:11 Django Features
    .
    3:56 Django Architecture
    .
    5:36 Prerequisite
    .
    6:01 Django Installation for PyCharm
    .
    1:36:47 Writing a test
    .
    1:57:07 Intro to HTML Structure
    .
    1:58:02 How Does HTML work?

    1:58:37 HTML Structure.

    2:04:22 Do's & Don'ts
    .
    2:10:42 Django REST Framework
    .
    2:11:12 What is an API?

    2:12:47 Introduction to REST API
    .
    2:13:42 What is REST API?

    2:15:47 Installation
    .
    2:32:52 How to Develop a Website From Scratch?

    2:45:47 Django Interview Question.

    I LOVE EDUREKA..!!!

    Hit Like..!!!!

Leave a Reply

Your email address will not be published.


*