what's going on everybody and welcome to
an update to the web development in
Python with d Jango tutorial series it's
been about 3 years since I covered D
Jango so I think it's about time to do a
little bit of an update plus everybody
had been asking for something a little
more in depth last time so I'm hopefully
going to kill two birds one stone so
here we have the Django homepage you
actually don't have to go here but this
is their website they've got
documentation all that definitely check
that out um they also have a pretty
decent tutorial that's worthy of
following as well especially if you guys
are uh getting confused on you know how
things work in Django and stuff like
that so D Jango is kind of a a high
initial investment for you um or at
least for most people because the
understanding how this whole model view
controller thing works and all the
connections that need to be made can be
kind of a challenge initially but I
promise if you stick through um it's
definitely worth it in the end because
as your project grows and you need to
make changes or add new things using
something high quality like Jango uh is
going to make it unbelievably easy for
you down the line it's just a little
harder to get started but hopefully I
can make it easier for you guys so let's
go ahead and get into it enough chitchat
I think the best way to learn jeno and
really anything is to just do it I'm
going to assume you guys know the basics
of python if you don't follow any Basics
tutorial you can go to python
program.net come to the fundamentals
boom there's a Basics series there or
anyone else's uh just just know the
basics of python okay so uh let's go
ahead and get started so the first thing
oh the a couple other things I'm going
to mention is I'm going to be using the
Sublime Text Editor you don't have to
use Sublime Text you could use any
editor you want you could use notepad++
or notepad for that matter or pie charm
or whatever uh use whatever you want um
the other thing is I'm going to be doing
some of the development on a Windows
machine and then later we'll be on Linux
server since eventually if you're going
to deploy a website chances are you're
going to be on Linux somewhere so anyway
um your operating system doesn't matter
I've also done a lot of development on
Mac I've done it on Windows and I've
done on Linux it just doesn't matter so
um we're going to be using python 3.7
here uh so also I'm assuming you've got
python installed already and with that
let's go ahead and get
started so the first thing I'm going to
do is hopefully not drool out of my
mouth and uh open up a command window
and we're going to go ahead and do a pip
install
Jango uh if you're on Linux or Mac you
might say instead like python 3M pip
install Django or something like that um
if you're on Windows it's pip if you
have multiple versions of python then it
would be like pi- 3.7m pip install okay
I'm assuming like I said though you guys
already know that so take note we're
using Django
2.1.5 here uh you don't have to use the
exact version I use just know that as
things progress things might be a little
different um from what I've seen a lot
of things are still backwards compatible
and it's like totally fine things aren't
getting deprecated but sometimes things
are so um if something's not working out
for you for whatever reason you could
try to install the exact same version as
I'm using to see if it's just a
difference in version so for example if
you wanted to you could also say pip
install Django double equals 2.1.5 and
that will install exactly version 2.1.5
now will just say before you deploy your
your creation
um make sure you're using the latest
version of Django uh there's lots of
things that change over time one of
those things is like security fixes and
stuff so if you're using an outdated
Jango you you might be making a mistake
so so the only reason you should install
a specific version that I'm using here
uh is just so you could follow along and
just learn about D Jango and then later
learn what what's the difference between
let's say Django 2 and Django 2.5 or
something like that but probably unless
this first number changes like Jango 3
something uh you probably will get away
with following along anyway let's see if
we're done with the we are done with the
install when you install Jango you get a
little command line thing called Django
admin we can do all these things with
Django admin but the main thing that
we're going to do is start a project and
um trying to think here that's probably
one of the main things that you're
actually going to use D Jango admin for
um apparently you can do quite a few
other things with Django admin but
instead you'll be using a different
script
so anyways um what we're going to say is
Jango admin start project and we're
going to call this project my site the
way Django works is it assumes all
websites are actually just a collection
of apps it could just be one app for
sure but it's probably a collection of
apps so consider a website that has like
a forum a store and like a blog or
something like that okay each of those
things are separate apps you have a
forum app you've got a store app and a
blog app okay that's how Django sees
things so when we start a project our
project is called my site and within my
site we're going to have a bunch of apps
that do things the thing that we're
going to do is uh we're going to create
a website that's like
pythonprogramming.net
I've been considering switching
pythonprogramming.net over to D Jango
currently it's written in flask and for
the reasons I mentioned before as the
web as as a project grows in flask
trying to add new elements and change
things over time uh becomes very tedious
uh with d Jango it it's just not so um
let me this always makes me think of a
com uh comic I've done it again have
let's see haven't wow if I could type
haven't TI and I bet if I go to images
hopefully we don't see yeah hopefully
nothing on here is what we shouldn't
have save search is on anyways this is
the comic I always think of you know you
start off a project you think you're
doing great and then at the end of the
day it it looks something more like this
and initially as we're setting up Django
it's going to feel like we're doing this
but the thing is that's as complex as it
will ever get um as opposed to a project
like pythonprogramming.net where there's
so many little connections going all
over the place um it really is a
challenge I I do think it's worth it to
use D Jango long term it's just hard to
initially get into it but anyways uh
start project my site boom let's create
my site and what's going to happen is we
should have a new directory we sure do
called my site this is your your project
right inside of your project you have
directory called my site and then we
have manage.py which is actually what
we're going to use to do much of the
things uh coming up so um inside of this
mysite directory this is kind of like an
app already um but I'm going to refer to
this there's probably a special term in
Django I just don't know it I'm going to
call this your like primary app
inside of this app really all this app
is going to do is point to your other
apps you're not going to use this you're
going to actually develop in here other
than to point to other apps and adjust
the settings other than that you're not
going to really do anything I'm just
going to zoom in a little bit so coming
back out to this directory we want to
play with manage.py so I'm going to open
up a command window here if you hold
shift and right click by the way um
that's how I get that you also if you're
on Windows can just do CMD and that
should also open up a command window
right where you are
so the first thing that we're going to
do is um start an app so I'm going to
say uh let's say pi- 3.7 I actually can
just say python because I only have one
version but anyways Pi 3.7 uh
manage.py start app and then we're going
to call this app main so this will be
like the main core of the website so for
me this is where like we're going to put
like tutorials like the main aspect is
tutorials and if anything I could
probably call this tutorials as well but
I'm probably going to want to put other
things inside of main besides just
tutorials so I'm going to call it main
for
now so we've created an app called main
if we come back we should see oh we've
got another directory here called main
if we open that up it looks a lot like
um our mysite directory or our primary
app but uh we've got actually some extra
things here um and as we can see we've
got models views where's the controller
we actually don't have any controller
here but we we'll get there I promise so
um what we want to do now is um let's go
ahead I'm going to get this just to stay
running now so what I'm going to say I'm
going to move have two command prompts
here first one I'm going to do
is um we're going to use manage.py and
we're actually just going to say uh run
server so run server and what this does
is runs your development server you in
Theory could run a whole website off of
this it just isn't recommended U
probably for all sorts of reasons
including security reasons anyway you
can see uh we've got some uh migration
issues that we can worry about later but
mainly we can see that the website is
now running at basically Local Host on
Port
8000 so uh let's head
there let's see what we got
paste okay so you get this little
beautiful imagery and uh it just kind of
notifies you hey you you things are
working sort of up to this point but
rather than having this kind of display
I think it's a little cooler to to have
you know control like how do I change
all of these
things so uh let's get into it so first
of all with the model view controller it
kind of works almost like in Reverse of
that you've got your controller which
basically takes the URL that whoever
typed in or clicked on a link that went
to that URL the controller takes that
URL and maps that to a view and then
that view Returns the template now most
of the time that view is also going to
interface with your models but it
doesn't actually have to really you just
need a controller in a view um to return
something so let's go ahead and do that
just so we can start to pick up how all
of this uh this stuff works so uh the
first thing that we're going to do is um
let's let's just create inside of main
here uh we don't have a urls.py so we
need to create one the other thing we
could do is go into my site take this
urls.py I'm just going to say copy that
and then I'm going to come into main
paste and now we have a urls.py
um I'd love to know like why it doesn't
by default come like when you start a
new app why it doesn't have a urls.py by
default if anybody actually knows uh
that'd be cool so this is like the first
time I'm using this uh this machine
so I've got to add Sublime Text really
quick to the thing that I want to open
great the other thing I probably need to
do before I forget inent using spaces
done okay so this is what um our urls.py
just make sure you're in main not my
site actually I think it would make most
sense to start
in um in the mysite URLs St High because
when someone first comes here what it's
going to do is it's first going to look
to your primary app and it's going to
look at that urls.py and then it's going
to see uh do we have a URL pattern that
matches and it needs to have a URL
pattern that matches but generally your
primary app is just going to have a
pattern that matches and points to some
other app so uh the first thing we're
going to actually do let me go file open
folder and then let's
open my
site and then I'm just going to close
this and I think this is how we're going
to interact from here out um so I'm
going to go into first let's go to my
site urls.py so primary app I switched
around don't get confused it's your
fault um so what we want to do here is
we just need to take this because it's
our primary app and point it to our new
app called main so we're going to add
another path here I'm just going to copy
this come down here and I'm going to
paste and then what I'm going to say is
uh we're going to well first let's also
import
include and then what we're going to sa
for this path is an empty path so
homepage no other URLs are being
submitted here we want to
include main. URLs so whenever someone
comes to just just the homepage enters
nothing else that this this path is
going to match so then it's going to say
okay let's go to main. URLs now so then
we're going to go to main
URLs make sure you're in main URLs and
now what we want to do is actually we
don't need admin here um so I'm going to
just delete that for now and then we are
going to say and we don't need this
admin either we'll keep path and then
the other thing we're going to do is
from just relative uh import views
and then we're going to pass uh an app
name and we're going to say app name
equals main this is useful later on when
you're trying to create custom URLs that
will point to a specific view but you
don't actually have to hardcode the URL
it's all totally Dynamic um so right now
we're not using that but I just don't
want to forget about it later and you
might as well just always throw this in
because eventually you'll want to have
that function and you'll try to use it
because you saw in an example somewhere
or something and it's not going to work
and you're going to be like wait what so
anyway we'll throw that in there for now
so then we're going to say uh path and
again the path would be homepage cuz
like right now all paths will lead to
this urls.py so but for now we're going
to say okay the empty path is going to
point towards a views. homepage and then
we're going to give this also a name
called homepage and I'm going to put a
comma here before forget because later
on we'll add more paths and uh throw a
syntax there if we mess that
up okay so going back over someone comes
here they visit the homepage first we go
to our primary app comes here it says oh
hey look here's a path that matches what
they just put in there's nothing else
here cool what do we do include main.
URLs fine boom points to main. URLs we
come here path ah okay this points to a
view and it's the homepage function in
that view okay great um unfortunately we
don't have um The View done so if we
come into our main area here and in fact
I said I was going to keep using this so
I think we'll keep using this if we come
into main we can click on views.py and
here we have uh what we need to bring in
views now render is used to render
templates I just want to pass a simple
string right now I don't want to get
into templates so the next thing I'm
going to say is from Django HTTP import
http
uh
response now Define homepage and then
we're going to pass a request so
basically all your actual views if you
have like a helper function or something
it obviously doesn't need requests but
for any actual view you pass request
always um and then obviously this is the
name that we passed right here right we
said views. homepage so that's why it's
called homepage now all we're going to
say is return 8
HTTP response and then we can pass
actual HTML here even so we could say um
wow this is
an tutorial and you can feel free to put
whatever you want in there I'm going to
say
awesome okay save that great so now
let's make sure our server's still
running did we we hid any errors I don't
see anything just yet so let's come over
here let us refresh the page boom done
let me zoom in a little bit just so
everybody can see how what kind of a
tutorial oh it's awesome tutorial okay
um I think this is a good stopping point
before we get into the next tutorial
which is where we're going to talk about
models and stuff like that and quickly
start to ramp up but the big thing is
you have to understand even just this
basic stuff because that was actually a
lot of work to go through just to return
some text on a page so what I strongly
suggest you do is after this video
before you go to the next one is try to
do everything we just did on your own
without needing to look anything up I
mean obviously you might forget some of
like the import names or something like
that but even then it's just dko HTTP
HTTP response um and then the only other
import you need is the uh include here
so uh you should be able to do this
completely on your own now and so what I
would suggest you do is create a new you
know start a new project and try to do
all of the things that we've done up to
this point without needing to reference
anything um obviously as you as time
goes on that won't be necessary that you
can like make an entire website without
being able to look anything up but it's
important that you understand how these
initial connections are made before you
continue on because it only gets a a
little more complex from here so anyways
um that's it for now a quick shout out
to my most recent uh channel members
denesh Kumar Luciano Cena Punit nathani
and UD
man Shanda uh thank you guys very much
for your support you guys are awesome
also side note this uh series is uh
sponsored in part by linode which is who
I've actually used to host
pythonprogramming.net
for like Years also they're my basically
they're my main host for like my larger
websites um and we'll be using linode as
uh as our deployment virtual private
servers down the line but for quite a
while we'll just develop locally um
because I think that's really the way to
go anyways but if you want to check out
Lenard or you need a hosting uh solution
I'll put a link in the description from
them um but later on we'll actually go
through how you can actually launch
ajango application on linode and
actually you know why something like
linode is better than uh some of the
other options that you have out there
trust me I've used all of them so
anyways um that's it for now questions
comments concerns whatever feel free to
leave them below also we have a Discord
server it's
discord.gg sentex in your browser get
you there uh come hang out come chat
with us ask questions whatever um tell
me how uh how I can improve my tutorials
all that kind of stuff anyways I will
see you guys in the next video