00:00:00
hey there how's it going everybody in
00:00:01
this series of videos were going to be
00:00:02
learning how to build a full-featured
00:00:04
web application using the Django
00:00:05
framework and Python
00:00:07
so Django is a very popular framework
00:00:08
that gives us a lot of functionality
00:00:10
right out of the box
00:00:11
and makes it really enjoyable to work
00:00:12
with these web applications so first let
00:00:14
me show you what will be building in
00:00:16
this series of videos and then we'll get
00:00:17
started learning how to actually put all
00:00:19
of this together so this is the
00:00:20
application that we'll be building here
00:00:22
and you can see that it's a blog style
00:00:23
of an application where different users
00:00:25
can write different posts now this can
00:00:27
be like blog post or they can be Twitter
00:00:30
updates or whatever it is that you want
00:00:31
to do with this now we have an
00:00:33
authentication system so I'm logged in
00:00:35
right now so we can log out and if we go
00:00:38
to register then new users can create a
00:00:41
new account if you already have an
00:00:43
account then you can login and we can
00:00:45
see at the login page we have this
00:00:46
forgot password link now that will allow
00:00:48
users to reset their password by getting
00:00:51
an email now if we have an account then
00:00:54
we can login so I'll say Corre EMS here
00:00:56
is the username I created and I'll login
00:00:58
with my password and now that we're
00:01:00
logged in we have a few different
00:01:01
options up here at the top so we can
00:01:03
view our profile and update our profile
00:01:05
information now we can also update the
00:01:08
profile picture that we have here so if
00:01:09
I go to choose file and choose a
00:01:12
different file and update that then we
00:01:14
can see that profile picture was updated
00:01:16
now that's also resized in the
00:01:18
background to save room on our web
00:01:19
server if that picture is too large so
00:01:21
if we go back to our home page then we
00:01:23
can view other people's posts so we can
00:01:25
view an individual post here by someone
00:01:27
else now if we view an individual post
00:01:30
that is something that we've written
00:01:31
then we also have the ability to update
00:01:34
or delete that post so if I go to update
00:01:36
and say my latest updated post and post
00:01:39
that then we can see that now that post
00:01:42
was updated with that new content so we
00:01:44
also have the ability to delete posts if
00:01:46
I click on delete then we'll ask if we
00:01:48
want to confirm that we want to delete
00:01:50
the post so I'll say yes and then back
00:01:52
on the home page we can see that that
00:01:53
post was deleted so that's a quick tour
00:01:55
of the application that we're going to
00:01:56
be building and building something like
00:01:58
this is a great way to learn the ins and
00:01:59
outs of a framework because you're going
00:02:01
to be exposed to so many different
00:02:02
things so for example we'll learn how to
00:02:04
work with databases and also how to
00:02:07
create an authentication system and
00:02:08
accept user input from forms and send
00:02:12
email
00:02:13
to reset passwords and all kinds of
00:02:15
different things like that now since
00:02:16
this is a django application we also
00:02:18
have the ability to access an admin page
00:02:21
if we have the correct permissions and
00:02:23
within here you get a nice GUI to be
00:02:26
able to view all of this back-end
00:02:27
information and update it on the fly if
00:02:30
you'd like so let me go back to the main
00:02:33
site here so we're going to be learning
00:02:34
how to add a lot of functionality to an
00:02:36
application within this series now I'm
00:02:38
gonna mention this several times
00:02:39
throughout the series but if you're
00:02:41
following along and would like to
00:02:42
download the source code of each step in
00:02:44
the process then I will have links to
00:02:45
the source code of each video in the
00:02:47
description section below so that you
00:02:48
can download those if you'd like and
00:02:50
also if you'd like to know how to build
00:02:51
the same application and another
00:02:53
framework then I also have a flash
00:02:55
series where I've created the same
00:02:56
application using the flask framework so
00:02:59
if you're interested in that then I'll
00:03:00
put a link to that in the description
00:03:01
section below as well also I'll be doing
00:03:04
a video in the near future where I
00:03:05
compare the two frameworks and when it
00:03:07
might be best to use one over the other
00:03:09
okay so let's get started with learning
00:03:11
how to build this application using
00:03:13
Django so I'm going to close down my
00:03:15
browser here and pull up my terminal and
00:03:18
if you're on Windows you can pull up
00:03:19
your command line so first off let's
00:03:21
start off by installing the packages
00:03:23
that we need to get started out so you
00:03:24
can do this in a virtual environment or
00:03:26
in your default Python environment but
00:03:28
it's always a good idea to separate
00:03:29
different projects into their own
00:03:30
virtual environments now I'm mainly
00:03:32
going to focus on Django in this series
00:03:34
so if you need to install Python or want
00:03:37
to learn how to work with virtual
00:03:38
environments or are wondering how I set
00:03:40
up my text editor or anything like that
00:03:42
then I'm gonna put links to those videos
00:03:43
in the description section below but I'm
00:03:45
not gonna go into those and detail in
00:03:48
this series I'm just going to assume
00:03:49
that you're specifically ready to start
00:03:51
learning Django okay so with that said
00:03:52
let's get started so first let's install
00:03:54
Django so to do this we can simply do a
00:03:56
pip install so I'll say pip install
00:03:59
Django and we can see it looks like it's
00:04:02
successfully installed Django 2.1 but to
00:04:05
be sure let's run a command and that
00:04:08
command is python - m django - - version
00:04:13
and that should display the version of
00:04:15
django that we are using so if that runs
00:04:17
then django was installed correctly now
00:04:20
you can also see that we're using django
00:04:22
version 2.1 in this series so be sure
00:04:24
that you're also using Django to point
00:04:26
1 or higher or else some of what you
00:04:28
learned here might not work in previous
00:04:30
versions now I'm also using Python 3.7
00:04:33
so you'll want to use a later version of
00:04:36
Python also if you can there are some
00:04:38
features that I'll be using in this
00:04:40
series such as f strings that are only
00:04:42
available if you're running Python 3
00:04:44
point 6 or higher ok so now we have
00:04:46
Django installed so now let's create a
00:04:48
new project from scratch so I'm on my
00:04:50
desktop here you can create this project
00:04:52
anywhere you'd like on your machine but
00:04:54
I'm going to use my desktop so to create
00:04:56
a new project we're going to use some
00:04:58
commands that are available to us now
00:05:00
that Django is installed and one of
00:05:02
these commands is Django admin so if you
00:05:05
type that in then it should show you the
00:05:06
available subcommands so if I run that
00:05:08
that is Django - admin then we can see
00:05:12
that we get a list of subcommands here
00:05:15
and we can see that there are a lot of
00:05:17
different sub commands listed here and
00:05:18
we'll see a couple of these later in the
00:05:20
video but we're going to use the one
00:05:22
right now called start project which is
00:05:25
right here so start project will create
00:05:27
a new Django project for us that has a
00:05:29
complete structure with different files
00:05:31
and everything else that we'll need to
00:05:32
get started so let's do that
00:05:34
so I'm simply I'll clear my screen here
00:05:37
and I'm simply gonna say Django - admin
00:05:41
start project and now we can specify the
00:05:44
name of our project so I'm just gonna
00:05:46
call mine Django let's do Django -
00:05:49
project and run that ok so that's not a
00:05:52
valid project name let's see if I use an
00:05:55
underscore instead ok so that worked I
00:05:57
always forget which one is which so you
00:05:59
can create a project with an underscore
00:06:01
but not a dash so I created a new
00:06:03
project called Django underscore project
00:06:06
and now that we've created that if I
00:06:08
look at my desktop then we can see that
00:06:11
now I have a directory on here called
00:06:13
Django underscore project ok so first
00:06:16
let's simply CD into this directory and
00:06:19
look at the project structure of what
00:06:21
that start project command just created
00:06:23
for us
00:06:24
so within my command line here I'm gonna
00:06:25
say CD into Django underscore project
00:06:29
and now I'm just going to open this
00:06:30
project and a text editor now I'm using
00:06:33
sublime text but you can use any editor
00:06:35
that you'd like I have a video on how I
00:06:37
set up my editor and that will be in the
00:06:39
description
00:06:40
and below if anyone is interested so I'm
00:06:42
going to open up sublime text here and
00:06:44
then drag our Django project into
00:06:47
sublime text and open that up so now
00:06:50
let's look at the project structure that
00:06:52
that start project command created for
00:06:54
us now I don't know how to make the text
00:06:56
over here in the sidebar of sublime text
00:06:58
any bigger so instead I'm gonna use my
00:07:01
terminal to look at this so let me open
00:07:03
the terminal back up here and clear the
00:07:06
screen now on my machine I have a
00:07:07
command called tree that helps visualize
00:07:09
this a little bit better so I'm going to
00:07:11
use that now you might not have this
00:07:13
command installed on your machine but
00:07:15
you could simply use your editor to view
00:07:17
this structure instead so I'm gonna use
00:07:19
that tree command okay so we can see
00:07:21
that we have a pretty simple structure
00:07:23
here now I'm going to open up my editor
00:07:25
and go through each of these really
00:07:27
quick so just so we can still see the
00:07:29
terminal and the structure here I'm
00:07:30
going to put this over here on the side
00:07:33
about right there and my editor I'm
00:07:36
going to drag over here and make this
00:07:38
take up about 3/4 of the screen here
00:07:41
okay so we can see here in the structure
00:07:43
that on the base level we have a manage
00:07:46
pie file and a django project directory
00:07:50
and that man is not pi file is a file
00:07:52
that allows us to run command line
00:07:54
commands so let me open that up here in
00:07:57
my editor and this is what that manage
00:08:00
dot pi file looks like now we'll see
00:08:02
this in action in just a second when we
00:08:03
run this default site but we won't
00:08:06
actually be making any changes to this
00:08:07
file so also in our base directory for
00:08:10
our project we also have a directory
00:08:12
called
00:08:13
django project which is also the name
00:08:15
that we used for our project itself and
00:08:17
within that directory let me open that
00:08:20
so within this directory we can see that
00:08:22
we have four different files the first
00:08:24
is this double underscore Anette dot pi
00:08:27
and if we look at that in our text
00:08:29
editor then we can see that that is just
00:08:30
an empty file so that just tells python
00:08:32
that this is a Python package so next we
00:08:36
have this settings dot PI file so if I
00:08:38
open that in the editor as you can
00:08:40
probably tell from the name this is
00:08:42
where we'll change different settings
00:08:43
and configurations so we'll be using
00:08:45
this throughout the series we can see
00:08:47
that most of these files have good
00:08:49
documentation and links provided where
00:08:52
we can learn more
00:08:53
information now if we get glanced
00:08:55
through here then we can see up here
00:08:57
towards the top that we have a secret
00:08:59
key and that just adds a lot of security
00:09:02
enhancements to Django now we can see
00:09:04
that we also have debug set to true here
00:09:07
we have an installed app section we have
00:09:10
some database settings here and all
00:09:14
kinds of different useful settings that
00:09:16
we will talk about more in future videos
00:09:19
but for now we're just taking a quick
00:09:20
glance so looking back at our project
00:09:23
structure we also have this URL spy file
00:09:26
so let me open that up now this is where
00:09:28
we'll set up the mapping from certain
00:09:30
URLs to where we sent the user so for
00:09:32
example there is one path pattern here
00:09:35
set up right now and that is for admin
00:09:38
so if we were to go to our site and go
00:09:40
to the route admin then it will send us
00:09:43
to this admin dot sites not URLs and
00:09:46
we'll see more about this in just a
00:09:47
moment when we add some additional
00:09:49
routes okay lastly we can see that we
00:09:51
have this whiskey dot pie file here if I
00:09:54
open this up in the editor so whiskey or
00:09:56
WSGI
00:09:57
is how our Python web application and
00:10:00
the web server communicate so Django set
00:10:02
up a simple default whiskey
00:10:04
configuration for us in this file but
00:10:06
we're not going to be actually touching
00:10:07
this file okay so that is a quick
00:10:09
overview of the project structure that
00:10:12
was created for us when we ran that
00:10:14
start project command so now let's
00:10:16
actually open up the default web site in
00:10:18
our browser unlike a framework like
00:10:21
flask we actually don't have to write a
00:10:23
single line of code to open up a basic
00:10:26
web web site in our browser we just have
00:10:28
to run a command so to do that we're
00:10:31
just going to use that manage py file so
00:10:34
to run our server that to access our
00:10:36
site we're just going to go over to our
00:10:38
terminal and I'll make this larger again
00:10:40
so that we can see everything and now
00:10:42
within that Django project in the same
00:10:44
directory where that manage py file is
00:10:46
we can say Python manage py run server
00:10:50
and that is a command that we're going
00:10:54
to be using a lot okay so you're likely
00:10:56
going to see this warning here that says
00:10:59
that you have 15 unapplied migrations
00:11:01
and then it tells you a command that you
00:11:04
can run to get rid of that warning
00:11:06
so let's not do that right now I'll
00:11:08
explain this migration stuff in just a
00:11:10
bit
00:11:10
but for now if we look here at the
00:11:12
bottom it says that our website is now
00:11:15
running and that we can access our site
00:11:18
by going to this URL here HTTP 127 0 to
00:11:23
0.1 port 8000 now another name for 127 0
00:11:28
dot 0 dot 1 is localhost it's basically
00:11:31
just our local computer that we're
00:11:33
currently on now this is actually a
00:11:34
running webserver so we have to leave
00:11:36
this running while you're viewing your
00:11:38
site in the browser or else you won't be
00:11:40
able to see it so let's copy and paste
00:11:42
this URL that it gives us and open this
00:11:45
up in the browser so I'm going to open a
00:11:47
new incognito window here and paste that
00:11:50
in to our URL bar and we can see that we
00:11:52
have what looks to be a website here so
00:11:55
this is the default website that Django
00:11:57
has created for us and in this series we
00:11:59
will modify this so that we have the web
00:12:01
application that I showed at the
00:12:03
beginning of the video so by default it
00:12:05
shows us that a page that says that
00:12:07
debug is equal to true and also it
00:12:10
provides us some links down here to the
00:12:12
Django documentation and stuff like that
00:12:14
now when I said that 127 zero to 0.1 was
00:12:17
the IP address of our local machine I
00:12:19
also mentioned that there's an alias for
00:12:21
that IP address called localhost and I
00:12:23
like using that more than the IP address
00:12:25
itself so if I replace that 127 does
00:12:29
zero to 0.1 with localhost then this
00:12:32
should still work so now we're at
00:12:34
localhost port 8000 and we still have
00:12:36
that same site ok so if you remember I
00:12:38
said that within our projects URLs dot
00:12:41
PI module that we had that admin route
00:12:44
so let's try to navigate to that route
00:12:47
really quick so if I go to Ford slash
00:12:48
admin then we can see that there's
00:12:50
actually something on this page we get a
00:12:52
login screen so let's pull back up the
00:12:55
project and open that URLs dot PI module
00:12:59
one more time here so like I said before
00:13:01
this is a way to map URLs to certain
00:13:04
locations so that they can be handled in
00:13:06
a certain way so in this case when we
00:13:08
went to ford slash admin then it's going
00:13:11
to admin site stat URLs and then the
00:13:14
logic within that location handles the
00:13:16
route further and we'll look at adding
00:13:18
more routes
00:13:19
but for now that is how that admin route
00:13:22
is working so the logic at this admin
00:13:24
dot site URLs that is what allows this
00:13:28
admin page here to work correctly now we
00:13:31
can't access this with any credentials
00:13:33
just yet because there is some more that
00:13:35
we need to do first but I just wanted to
00:13:37
show you that the URLs and that URLs
00:13:39
module are already routing us to
00:13:41
different locations and in the next
00:13:42
video we'll see how to add some more
00:13:44
routes to that module so that we can
00:13:46
show users exactly what we want them to
00:13:47
see when navigating to different areas
00:13:49
of the site but for now let's open our
00:13:52
terminal back up and stop running our
00:13:54
server so I will pull up my terminal and
00:13:57
you can stop running your server by
00:13:59
hitting ctrl C and sometimes I have to
00:14:02
hit that twice to get that to stop for
00:14:04
some reason but just hit ctrl C a couple
00:14:06
of times and it should stop now I will
00:14:09
clear our page now with that server
00:14:11
running and debug mode like it was it
00:14:13
should automatically reload any changes
00:14:15
that we make to our code but if you ever
00:14:17
have any trouble with the website not
00:14:18
reloading for any reason then you might
00:14:20
want to stop the server just like we did
00:14:23
and restart it by running the command
00:14:25
that we reran earlier so to bring it
00:14:27
back up we can just do python manage py
00:14:30
run server and that runs the server
00:14:32
again ok so I think that's going to do
00:14:34
it for this video in this video we
00:14:35
learned how to get Django installed and
00:14:37
how to create a new project we also
00:14:39
looked at the structure of what gets
00:14:41
created and how to pull up that default
00:14:43
site in the browser so in the next video
00:14:45
we'll learn how to create an application
00:14:47
for our site and set up some basic
00:14:49
routes now that might sound confusing
00:14:51
right now that we'll be creating an
00:14:52
application within our current project
00:14:54
but we'll see what that means in the
00:14:56
next video but if you have any questions
00:14:57
about what we covered in this video then
00:14:59
feel free to ask in the comment section
00:15:00
below and I'll do my best to answer
00:15:02
those and if you enjoy these tutorials
00:15:03
and would like to support them then
00:15:05
there are several ways you can do that
00:15:06
the easiest ways to simply like the
00:15:07
video and give it a thumbs up and also
00:15:09
it's a huge help to share these videos
00:15:10
with anyone who you think would find
00:15:11
them useful and if you have the means
00:15:13
you can contribute through patreon and
00:15:14
there's a link to that page and in
00:15:15
description section below be sure to
00:15:17
subscribe for future videos and thank
00:15:18
you all for watching
00:15:20
you
00:15:29
you