>> Hi there, I'm Christopher.
If we're going to have a demo set of videos,
we're going to put together a beginner set of videos,
we're of course obligated,
required by law almost to do Hello World.
Let's do exactly that.
In the prior two videos,
we saw how we could install Django and
how we could set up our project parent and our app child.
Now, we want to start to add in
a little bit of code to do that Hello World.
But of course, before we do that,
we're going to need something to serve up our website.
Fortunately, we've already got that built in.
That if you remember the manage.py file,
that entry point for our scripts, well,
that includes among other things,
a little script called runserver,
which will start up our server.
In our case, because we've got that debug flag set to true,
it's going to set up a dev server for us.
Let me run this,
and what you'll notice is we now get
this big nasty error message telling us we
have 18 unapplied migrations.
Don't panic, it's okay.
Right now, that's irrelevant.
It would become important if we were working with the database,
but we're not there yet.
For right now, we can ignore that.
Instead, let's just open up the server.
You'll notice it's created that right there.
Control "Click," it opens up in my browser,
and there is that default Django landing page.
It tells me that my install work successfully,
gives me a nice congratulations. It's always nice to see.
Then it tells me you're seeing this page because debug is set
to true and we have no configured URLs,
which is also true.
Let's break this down a little bit and then
that will then set the stage for where we're going to go.
It tells us that we don't have any configured URLs or
URL confs as they're known in Django terminology.
In a lot of other frameworks,
you'll hear the phrasing your route and
that's honestly the one that I like to use.
In a nutshell, what routing or
a URL conf is all about is a user makes a request,
they go to a particular URL,
they do that by opposed by
submitting a form or maybe get by clicking
a link and we now need to run code in response to that,
so we need to route their request over to some function or
some method that's then going to perform the desired action.
That's how we're going to walk through this,
that we will setup first that little function,
and then we'll go ahead and create that route or that URL conf,
which will then act as the middle ground,
it will bring everything together for us.
I'm going to open up Visual Studio Code here,
and I'm going to go into my app or my relecloud folder here,
and I'm going to go into views.
Views is where we will add in
the code to display information out to the user.
That's what a view is all about.
It's how the user's going to view some information.
In my case, I'm going to create a function here.
We can actually do classes as well,
but that's a conversation for a different time.
For right now I'm going to focus in on
my function and I'm going to add in one parameter called request.
Request will have all the information
from the user and some other really neat stuff as well.
So we'll have thing like all the cookies that were sent up,
the form data, the query string.
Basically anything at all that's part of
the request is going to be inside of that parameter.
For me right now though, I don't need it,
so I'm just going to ignore it and instead,
I just want to send information down to the user.
I just want to send down a response and I'm actually going to use
a slightly different helper function here called HttpResponse,
which will allow me to send down some basic text that it
will automatically wrap up as a response.
I'm going to say return HttpResponse, Hello, world.
Perfect. There we go.
Because again, it wouldn't be
official if we didn't do Hello World.
I'm going to return that value back.
That's the first part.
There is now our view that
we've created and we've done this with a function.
But the other part over here is now going to be that route.
How are we going to take the user request and send that over?
Again, remember that that is done
by using a URL conf or URL configuration.
You might remember from earlier that inside of
our project we have one global URLs py file.
This is going to be for our entire website here,
so everything is going to be routed to
hear first and then this will then
go ahead and either maybe route directly to a particular view,
or more commonly, it will
route or send this out to a helper URL's py file.
Now you'll notice that I earlier added in
a little comment here that this is my global one,
this is for my project,
and I just like to tag that because sometimes it can get
a little bit confusing as to which URLs py file you're in,
especially because they'll typically have the same name,
so with that in mind,
let's actually create a brand new one.
I'm going to go back over here at relecloud,
and I'm going to create a brand new URLs.py file.
You might be wondering,
well, wait a minute, Christopher,
why not just put everything inside of that one?
Think about your websites that your routing tables are
typically quite large because there's a lot of
different requests that need to be mapped
into different places and so forth.
So putting that all into one file,
it's probably going to become unworldly after awhile,
so we'll create separate ones
to manage different portions of our website.
I'm going to create one for my relecloud app.
This will be for my app,
which in my case is relecloud.
There we go, spelled with an app.
Now, I want one big helper function.
I'm going to say from Django URL's
and that's going to be import path.
Path is going to allow
me to identify how we're going to do our route.
I'm going to create a brand new list here that's
going to be called URL patterns.
You'll notice that I'm using a little extension here that has
a set of snippets built-in that
I could take a little bit of advantage of,
and that's inside of the extensions.
If you go take a look for
Django extensions for VS code, you'll see that there.
Now this URL patterns,
this is a convention name,
it does need to be URL patterns here.
That's what I've got setup.
Now inside of this list,
I can start adding in all of
the different paths through all of my different routes.
The default is going to be my empty string,
the second parameter is where we want to send this to,
and that's going to be over to our index view.
Let's pull that in. I'll say from.import views,
and then we'll say views.index.
Then I'm going to give this a name.
Let's give this a name of index.
Right now it doesn't really matter since I've only got the one,
but eventually you're going to need to
have the ability to look these up by name.
So it's good to get into that practice right now.
I've now created my URL conf for relecloud.
Now I need to register this inside of the global one.
Let me go back over here to my project,
and let's bring in one more helper here called include,
and now let's register a new path.
Again I'm going to say path and again,
empty string for my default,
and then what I'm going to say is basically ''Hey, look,
if it's not admin,
just route everything else over here to that
relecloud URL's py file up here.''
What's going to happen is my request will come in,
it's not going to match Admin.
It's going to be an empty string.
We'll come over here, and then
it's going to get routed over to my URL's py.
Then we will see that path here,
and then we will go, okay,
I need to call this index function,
and then that will be routed over there.
That's how all of the magic behind the scenes is going to happen.
All my files are saved.
Let me come back over here to my browser,
a real quick refresh and now we've got Hello, world.
Now this is an official beginner demo and video series.
That is how we can create a view,
how we can set up a URL conf, or route,
and how we can register all of that inside of our project.
[MUSIC]