>> Hi, I'm Christopher Harrison,
Senior Cloud Advocate at Microsoft,
and what I want to explore in this video is how we can set up
a view that's going to retrieve data by using the Django ORM.
We've already set up some of the base structure that we've got,
our base HTML file, our base template.
We've got the data
already set up and we've got a couple of core route setup,
we did that in prior videos.
I'm going to focus in on creating the view,
on registering the view inside of our urls.py file,
and then setting up the template to go
ahead and display out that information.
Let's start by setting up that view here.
Let me go back here to my views.py file,
and let me add in
a brand new view to display out our list of destinations.
I'm going to call this destinations,
there we go, and take in the request object.
Then first thing of course,
is I need to go load up our destinations.
Let me bring in our model,
so I'm going to say from.import models,
and now let's come down here and we'll say all destinations
equals models, destination, objects, all.
That will load all of them.
If we wanted to filter,
do something else like that, we could do that here.
I'm going to go grab all of them,
because I know that there's not a lot.
Now, I'm going to do the same thing that we typically would.
I'm going to say return, render the request object,
and then the name.
Let's call this destinations.html,
and now things are going to change slightly.
Before, in a couple of earlier examples that you can see up above,
what we did was we returned back a static HTML file effectively.
Now what we want to do is have this be dynamic,
we want to pass in some data.
So the way that we can do this is by setting up
a dictionary and passing values into that template,
that it will be able to use.
I'm going to give it a list of destinations
and I'm going to set that to be my all destinations.
Cool. Now, that's going to be passed into the template.
Let's go ahead and build that up.
Let's build up the template,
let's do that since we've just done this.
Let me go into my templates here,
and I'm going to create this destinations.html.
There we go. Let's go ahead and extends, again,
taking advantage of that great little extension here,
so base.html.
Our first block in this will be our title,
and let's call this ReleCloud Destinations
and then our next block.
This is going to be our content,
and now we're going to display out our destination,
so let's say h1 ReleCloud Destinations and close that out.
Now I want to loop through all of our list of destinations.
If we remember, we pass this in calling it destinations.
What you're going to notice is that,
just like we've got these curly braces to do
things like block and unblock and extends\,
we can also do a little bit of logic here
by using things like for loops and if statements and so forth.
You'll notice, let me again take
advantage of the cool little snippet,
is that I've got a for snippet.
So let me just hit that,
and you're going to notice that this behaves really just
like a for loop inside of Python.
For destination, in destinations, there we go.
So again, we call that destinations, so destinations,
and now we'll be able to use this just like we would
a normal object inside of Python Django.
It's a destination object
that's been loaded out from the database.
Let's go ahead and add in the display.
I'm actually going to go to the outside here real quick,
I'm going to say ul class equals and let's say list group.
This is just a bootstrap thing
just to make it look a little bit prettier.
Close out the ul,
and now let's go ahead and,
li class equals list group item,
and now we want to display out the destination.
Now, if what we want to do is to display a string,
so a dynamic string,
I want to display out the name of the destination.
Then the way that I can do that is by using the double curls
or the handlebars as
some other frameworks might like to call that.
So now, it's going to display whatever string it is that I provide.
In my case, I'll be able to say destination.name,
and that will provide the name of the destination,
and that was set up when we set up our model before that.
I can pull that up real quick here.
There you go, there's our destination and there's our name.
It's going to display out that name there.
Okay, cool.
This is now going to display out our list of destinations.
Now, I do need to register this again.
Remember that's going to be our typical pattern,
you're going to set up that view,
you're going to set up whatever the template
is and we need to register it.
Let me go to my apps,
urls.py, and let's add in destinations here.
I'll say path, and I'll say destinations,
then from our views this is going to be our destinations,
then let's give this a name of destinations.
Keep it consistent there.
Let me save that.
There we go, that's now updated.
If I go to my page here,
and I update my URL to be destinations and hit
"Enter" now you're going to
notice that we see out all of our destination.
There's all the destinations that we're inside of our database.
That's how we can then loop through all
of that inside the template and display that out.
The last little thing I want to do though,
is up here on that destinations link,
I want to make sure that that's pointing in the right direction.
Let me update my base HTML to do exactly that.
Back to my base HTML and let's go find
that destinations and then let's go in and say url,
tab to again take advantage of
the snippet, destinations, hit "Save".
Now let's go ahead and let's just go back home,
and then destinations, and if I click on that,
there is our destinations.
That is how we can add in a view that's going
to accept some data and also how we can load up data as well.
This is now how we are going to be able to access
the value that was passed
in and be able to loop through all of that.
You'll notice that we can now just use
that as we would any normal object,
and then just like we've seen already,
we were able to register that inside of our urls.py.
I like having the trailing comma,
some people do, some people don't. I like it.
Now we've been able to add that in and then last but not least,
we can update our base to have that URL.
That is how we can create a view that's going
to bring in data inside of Django.
[MUSIC]