>> Hi there. I'm Christopher Harrison,
I'm a Senior Cloud Advocate inside of Microsoft Cloud and AI.
What we want to start to explore in
this video is the concept of views inside of Django.
Now, one thing that we want to understand about any web framework,
regardless of which one that you're working with,
is it's like creating really any other type of application.
That if you think about, for example,
a phone app, when a user taps on a button,
then what's going to happen behind the scenes is
that your code is going to run some method,
it's going to run some function.
That's exactly what happens whenever you're working
with any web framework and Django is no exception.
What we then need to do in essence is we need to
create that function or
that method that our application is going to call.
We then need to make sure that
we wire it up since we don't have a button,
we need to explain to the framework, "Hey,
when a user does this,
this is the function that I want you to call."
Then we need to determine what it
is that we want to display out to the user,
so three steps there.
Create the function that we want to have called,
make sure that our framework knows when to
call it based on some user action,
and then determine what it is that we want to return out.
Now, I'm going to in this video,
take a look at how we can set up the function and
how we can set up what we're going to display out.
In the next video actually,
we're going to explore how we can tell our framework, "Hey,
when a user does this,
this is the function that I want you to call."
What I've done here is I've opened up the little starter project,
I'm inside of my Rele Cloud App.
I'm inside of the provided views at PY file.
This is what you'll see inside of any app
that you create inside of Django,
that first time is this empty views.
It tells us, create your views here,
let's follow those directions.
Now there's actually two ways that you can
create views inside of Django,
you can either do this with
a class or you can do this with a function,
let's start with a function.
I'm going to create one club index here,
which is typically what you're going to
call whatever that default page is,
then I'm going to say return render.
Then the request object that we got that contains everything from
the user and the name of the HTML page that I want to return back,
let's call this index.html.
Let's go in and create another year,
let's go ahead and say maybe the about page.
I'll say def about,
and then it's the exact same thing.
We accept the request object,
that's all the information from the user and then we
return back the results of this render command,
which is going to take our request.
Then, the name of the HTML file that it's going to display.
Now the HTML file that it's actually going to
display is going to be in Django terms,
what's known as a template.
As we're going to see a little bit later,
we can actually make this dynamic.
But it could just be
a static HTML page that that's all
that you need to display out, then that's it.
Let's see how we could do that.
Inside of my relic Cloud App folder,
I'm going to create a brand new folder
that I'm going to call templates.
In here I'm going to create my index.html.
Then I can just go ahead and create a normal HTML file.
HTML, head, title, Rele Cloud, like that.
Close our title, close head,
open up our body and I'm going to go ahead and cheat
here and just paste in a bit of text.
Let's go ahead and just paste that in there we go,
close out the body,
and then close out our HTML, cool.
I can do the exact same thing with the about page.
Let's actually just copy all of
that and let's create another template
here called about.html and let's just paste that in here.
We'll update our title and maybe to be about Rele Cloud.
Then we'll update our text again,
I'm just going to cheat here.
Let's just grab that bit of text, there we go.
What we now have is we now have our template here.
Again, right now they're just static HTML,
but as we'll see a little bit later,
you'll be able to make these dynamic.
We've got our normal HTML, head, title, body,
just like we would for our index and for our about.
We've got our couple of views created.
When index is called,
it's going to return back that
index.html page when our about is called,
it's going to return back the about.html page.
Of course, the next step is to tell Django to tell the framework,
"Hey, when a user requests this,
this is what I want you to do in response
to actually connect those."
That's actually what we're going to see in the next video.
[MUSIC]