Welcome everyone to this lecture, or we're going to discuss how to connect data based off the models
to templates on the frontend.
Now, I want you to keep in mind, you have a lot of the knowledge and tools needed to make a fully
functioning website.
You already know all the details necessary to connect a user to a backend database and have the user
interact with data things like create, read, update and delete.
It's all a matter of just creating these custom views that use commands from models, not pie.
The ones we just talked about and then actually send that information or retrieve that information from
a template.
You can retrieve it through an HTML form, or you could just send it back using the Django template
language that we've already learned about.
In this lecture, we're going to be exploring how we could report back information from the database
to the user in a template.
However, we really want you to keep in mind that there's still two major ideas we have yet to learn
about in regards to Tango.
And those are two major features we still have to learn are Django Forms, which allows Django to automatically
create forms from Python to template and class based views, which is extremely powerful tool that automatically
generates views like a detailed view or a list view based off a model.
So it's going to just generate a whole lot of work for you simply by using that model class.
So these are extremely powerful tools that are going to save you a ton of development time.
We just need to learn them first.
So these two features are powerful enough that I would suggest you learn them first before jumping straight
into using Django based off what you know so far.
I only warn this because a lot of times students basically see they have everything they need so far,
and they jump straight into making websites using just function based views, which is totally fine.
But there's still a lot more capability that we have yet to learn.
We're really just scratching the surface of what makes Django such a great web framework to work with.
So those features that is Django forms and class based views will drastically reduce her development
time once we learn them in upcoming sections of the course.
So I just want you to keep that in mind.
We still have a lot to learn, which is why we haven't really felt like we've quickly made a kind of
Real-World website yet.
OK.
For right now, though, let's show a simple example of how we would actually connect a template and
that could be used to report back information from a database and head over to the same file we've been
working with.
OK, here I am inside visual studio code.
We've been messing around with the model's top pie file a lot.
But let's go ahead and see if I could take all the patients I have and list them on some sort of URL.
First thing's first, let's actually connect a URL that lives inside of office.
So inside of office, I'll create a new file called URLs dot pie.
And remember, I have to actually connect this URLs that profile to my site level URL, I'll stop by
file.
So if I come to your stop, I remember this is the project level one has all this information and it's
actually connected with the URL configuration I will have for office.
We already know how to do this.
We've done it a bunch times.
So on, say its path.
And let's make this route.
Say Office.
Forward slash and then what I'm going to say here is I'm going to include and we're going to say office,
you are else, and if I'm going to include office thought URLs I need to import include.
OK, go ahead and save that change.
And now what I need to do is actually set up my URL patterns as well as a template for the actual office
here.
So what I'm going to do is come back to your URLs that pie and then say from Jane, go dot URLs, import
path.
And then we're going to say all patterns.
And we're going to make a list here that's going to be a path.
And if you ever get confused on, well, how do I define this path inside my application?
Remember, the instructions are actually at the project level that if you're using something like a
function based view, you can just say path, the actual URL path and then the view.
And if you want, you can give it a name.
So.
We are going to have this let's just call this essentially blank.
So that way it shows up on where essentially it's going to have something like this our domain.
Dot com forward slash office.
And then this in turn, is going to list all the patients.
So very simple, but hopefully it's kind of get your gears turning.
So how do I do this?
I first have to actually have a used up file, which I do instead of office.
Remember, I have views here, so here I'm going to actually create a simple view.
So we're going to call this view list patients.
And it takes in a request.
And then it's going to return, and I'm going to say return nothing right now, but you'll see later
on we are going to return something.
Obviously we're going to return a rendering.
But we'll come here and say this takes in from views.
So say.
From the import views.
And then we'll save use that and then we'll call list patients, and if you want to give this a name.
List patients.
It's kind of up to you.
OK, save that change.
So what do I have so far?
I have my project level URL, so you can kind of say I'm working from the front backwards that is pointing
to four slash office which calls office thought URLs, which in turn says, OK, if you want to see
four slash office, which eventually I hope will show a list of all the patients, then I'm going to
have a path here.
It's going to say you list patients and that name of that URL is less patients.
So what do I then need to do inside of use right now, returning nothing.
But that's probably not what I actually want to return.
Instead, I would like to render.
This remember, it takes in the request and then the template name, as well as the context that you're
passing back.
So how can we actually do this?
Well, the first thing to do is actually grab from models, all those patients, so in order to use
patient here, I need to inside of views say.
From Dot's import models, so this is one way to import it, the other way you could import it is just
say from that model's import patient.
It's kind of up to you.
But now what I can say is all patience is equal to.
Models, dot, patient objects, dot all.
So remember, this is essentially the exact same code that we've been working with, except this time.
I'm just doing it inside of a view.
And again, hopefully this gets the gears turning off.
Everything we just talked about can be done with any function based view, and I can either take in
requests or send them back.
So I'm going to go ahead and then render this.
With the request, I need to decide on the actual template.
So right now, we'll kind of put a placeholder here or say office and then forward slash list each HTML.
And then the other thing is, I need a context to pass it and remember, the context has to be a dictionary.
So let's make this.
And you don't really have to do it this way and so many steps, but I just kind of showing you what's
up here.
So context list is going to be equal to.
And then I'm going to say the string patients.
And they're going to pass back all.
Patients.
OK, so what is actually happening here?
I'm creating a context list, so to speak, that remember in the actual template, I can then refer
to this as patients, and it's going to have this patient's query set for me so that in here I'm going
to say the context I'm sending back is equal to context.
List, don't be confused by the fact that in this context, underscore list, that's really a reference
to the all patients that live in there versus the actual context dictionary.
In fact, if you wanted to, you could just call this context.
And send it back, and what's so great spelled context incorrectly there.
So again, all I'm doing is I'm grabbing all those patients.
I set it up in a dictionary that way I can send it back as this context object.
And then in my template, I'll be able to refer to patients here.
So again, we're kind of working backwards from what the user actually it starts off with.
And now we in a sense, go all the way back to the front and make sure that this list that each HTML
is ready to accept this context.
So what do actually put that well inside of office?
I'm going to set up my templates folder, so I'll say office.
New folder.
Templates and then inside of this, I'm going to say templates, can you folder here?
Office.
So remember, there's kind of this weird discrepancy, they have to say office templates, office.
You don't necessarily do that, but it is better to do it that way as we discuss story and templates
lectures.
And then inside of office, I'll create a new file and it's going to be called lists start each HTML.
So lists that each HTML.
That's just this HTML file that this is referencing.
I remember it expects a context that is going to have patients, and we already know I can use Django
templating to actually create this using an HTML list.
So let's come back up here and I'm going to set up my doc.
And so let's say documents here, and then I'm going to set up just a very single or very simple Django
template.
Remember, I could be expanding this I could be using based on HTML, I could be using content blocks
and more.
But we're just doing the very basics.
So I have this list of HTML and it expects a context being passed back from views, which is simply
going to call just patients here.
So to keep it extremely simple before we actually make it a little cleaner.
I'm just going to put in.
Patients here, so I'm able to call patients because that's what it is inside this context.
So I'm going to save that change inside of lists that HTML.
Go ahead and save that.
And now let's go ahead and run the server, I'm going to say Python.
Managed not pie run server enter.
And it looks like I made a small typo.
It should have been your URL patterns, not your URLs patterns.
That's OK.
Come back to your URLs and let's fix that.
That shouldn't have been in there.
OK, go ahead and save that change.
And let's run it again.
Looks like we are now good to go.
So I'm going to copy that.
And let's check out that website.
And remember the view or the URLs happening just on office.
So if you just go to that your office, you should see something kind of ugly, but it is actually reporting
back the query set.
So what I would like to do is actually report this back as a list using jingo template.
So I can do that with a simple form.
I can actually loop through the items in this query.
And this is known as a list view of your model.
Remember that idea that models are going to have really common views, like listing all the entries
in a model table database or filtering and then listing them all?
That's going to be something you can do really easily a class-based views, but right now, let's go
ahead and kind of do it by hand.
So how do I set up the jingle for Loop?
We're going to go back to let's start each HTML.
And then and here, I'm going to delete this.
And I will say for.
Let's say person in.
Patients.
And let's remember that if that's actually what it's called, come back to views, and yes, I'm calling
them patients.
So I'm going to say for person in patients and then here I have to end the four.
So say and for.
There you go.
And then the other thing I need to do is make sure that this is going outside of an ordered list or
it doesn't really matter.
But a create an unordered list here may exit that.
Paste it here, and then I'm going to create a list item for every person there.
And let's see what that looks like just by itself.
If I just say list the persons, so I'm going to say L I.
And then here.
We are going to say.
Person.
So save that change, and now we're going to.
Let me refresh this, I'm going to say Python management server again.
And let me bring back that page once it's refreshed, you should see something that looks like this.
It is essentially showing you the string representation of those objects.
Smith Susan is 40 years old.
Bullis, Mamie Smith, Adam Marx, Carl, cetera.
Let's imagine you only wanted one attribute here.
What can you do?
Well, remember these are just objects, so I can say person and I can say last name because these are
the attributes of the model.
And then when I refresh that, if I come back here now, I only see the last name.
So hopefully it starts getting again, the gears turning and you realize, OK, I have a lot of flexibility
with the model objects that I sent back.
OK.
So that is pretty much as far as sending information from the database to the actual template views.
I want to stress here that there are much better ways of doing this once we understand Django forms
and class based views, which is why I want to show you Django forms first before we kind of try to
messily hard code some Tema forms to send data the other way around.
It definitely is possible, but it's just much easier, much cleaner, much better with Django forms
instead of the raw HTML forms.
So keep this kind of in the back of your head.
You still have a ton of capabilities right now, but your life is going to be a lot easier once you
learn about Django forms and class based views instead of these kind of function based views, even
though they can't handle the job and from time to time, you will need to use them.
But there is a lot more to come.
Let me quickly describe everything we just did here, and let's again start kind of backwards.
So what did we do on the project based Earl's file?
I said, OK, connect this to my office URLs.
No problem.
Then what do I do in the office URLs?
File came back here and I said, connect this to views list patients.
Then I said, OK, well, now I have to create a view called list patients come back to views.
And this view is very simple.
It just says, OK, import the patient model, and then I can use all that stuff we discussed about
creating, reading, updating the leading, etc..
So then I grab all patients.
I send it back as a context dictionary, and then I render that inside of list of each HTML.
And then after I set up the directories for lists that each HTML, I can use the Django template language
that we've already learned about to display this however I want.
OK.
So I know we learned a lot, but hopefully you get to see how everything's starting to connect.
Thanks.
And I'll see you at the next lecture.