What Are Django Templates?
all right there hos welcome back and in this video we are going to be learning how to make templates. Now a template is probably exactly what you're thinking about. So if you just go online on Google and type in like shopping template or blog template or I don't know whatever template you're looking for, basically what they are are these generic HTML documents with a bunch of dummy data in it. So if you're looking for like a store template, it would have like a dummy item and it would have a heading that says put Title Here, put item description here, yada yada tomato tomato.
So the reason that we use those templates in Jango is because it allows us to pretty much separate our HTML from our backend stuff. And that way you can have a web designer and you can say all right make me a structure for you know the homepage or the album detail View and they can just make something generic. And we can have that in one file and then later on we can go and fill in all the information we want to such as put in the actual album title where it's supposed to be so on and so forth. So if it sounds confusing, once I set it up you guys are going to be like oh that actually is uh not that confusing and it's very useful.
Preparing the View to Load a Template
So actually let me go ahead and delete all of this HTML and code because that's what we're trying to avoid. And all right so the first things first. Anytime you want to work with templates in jeno just go ahead and right from Django and template import loader. So we're gonna make this template a separate file and we basically need to load it in, so load files whatever.
Alright now where do I want to stick this? How about under here. Alright so I'm just going to make a variable for our template whenever we load it in and that is loader.get_template(). And wow, we didn't even create a folder for templates yet so might as well should probably do that. And let me just go ahead, in this video I'm going to create a template for this homepage and it's going to be the most boring template you've ever seen but this is where you put it. Who is texting me? Someone's calling me from Evans Mills. Evans Mills is this crappy town next to me and there is literally nothing there so it's kind of scaring me right now.
Creating the Template Directory Structure
Alright so the convention is in your apps root directory go ahead and right click and make a folder called templates. Now you can actually change this in your settings but this is kind of standard convention. And also another thing that you do is you actually rightclick this and make a new subdirectory with the same name as your app. So music right in there. Now in here is actually where you put all of your HTML files. So again I'm just making this for the homepage so I'm going to name it index, and do I want want to add it to get? Sure why not.
Writing a Basic HTML Template
Alright so this is actually the thing that's just going to return the HTML just like we have been doing. So let me go ahead and make a really sweet template right now and it's going to be really leaving me messages huh?
Alright so for each album we'll just have it as a list item instead of just having line breaks so we'll just say list item and then of course we need a link. So hrf and then your url goes there and then the album, I'll say album title here. Alright so this is the most boring template you will ever see in your life but this is basically what it is. So our front end developer designer is going to give us something like this. Hopefully it's a little bit better unless they want to get fired. And then we have to plug in all of the pieces. So let's go back into our view and figure out how the heck to do that.
Loading the Template and Creating Context
Well the first thing we're going to do is for this variable right here we're actually going to need to load in this template file. So that is in the music directory and index.html. Now again the reason that we don't write templates/music.html is Again by default Django is already set up to look in a directory called templates in your apps directory. So make sure that you have that set up correctly. And now this template variable is a reference to this file. Pretty cool.
So the next thing we're going to do is we're actually going to create a dictionary. So whenever we pass this album information into our template we pass it through a dictionary. And most people name this context. You can actually name it anything you want but it's kind of the standard. It basically means information that your template needs. So you can also name these name and value pairs anything you want but I always keep them the same as just whatever I'm using in my python code, makes it really easy. So all albums is just equal to all albums. There you go.
Rendering the Template with Context
So of course the rules don't change. Whenever you have a view you need to return something. And that is whenever we, you know, analyze everything and plug in our data, we're going to return this final template. So how do we write that? If we just go to template.render, inside this render function go context and always pass in your request.
So again what we're going to do is we're going to pass this information into our template, we're going to put all the pieces exactly where we need to be, and then this is pretty much going to get converted to final HTML that we're going to return to the user. So now what we can do is we can hop in our template now that everything is good to go right here and start plugging in those pieces. So all we have to do is say okay the music slid goes here the album title goes here and we need to make a little Loop so it Loops through each one and just doesn't display one album.
Using Django Template Language for Loops and Variables
So here is the special syntax that you use whenever you're using kind of uh Jengo in an HTML document. Whenever you are using kind of think of it like python code you use something like this. Alright so you surround your code with squiggly bracket percent sign percent sign squiggly bracket. So since we're going to want to Loop through each album in what was the list, all albums, we're going to write something like this: for album in all albums. And then anytime you want to end your for Loop you do the same. And just write end four. So again all kind of, I want to say it, Jango code is going to go in these special symbols.
Now there's one exception and that is whenever you're using plain variables. So for example in this hrf remember the structure for this was music slash the items ID. Now this ID is actually album variable an album attribute. So that's going to need to be a variable as well. So you see already autocompleted for me. So I just say album.id and that's it. That's all you need to know about working with templates. Anytime you have python code then it goes in between these symbols. Anytime you're working with variables that you can just display as is they go in between double squiggly either left or right or beginning or ending whatever you want to call it. So that looks good and also we need to have a variable for this title as well. So we can just copy this and instead of ID it is just album title, I think that was the attribute name. And all right mate, looking good I think.
Checking the Result and Adding Conditional Logic
Alright so now let's go ahead and refresh our page and check it out. And we know that it works because they are no longer just playing links before, they are now part of an unordered list. And we could see that everything worked correctly and we can click those. Looking sweet.
Now one other thing that you may want to do and this is just kind of bonus, is before you display this list you just want to make sure that you have some albums to display. So maybe the user didn't add any albums yet. Instead of just displaying empty list then they're going to be like uh is the website broken. We just may want to have a popup that says um no albums are available or hey add some albums to your list or whatever. So in order to do that, let me go ahead and indent this. Alright so we need a if else statement. So again these special symbols right here since it's kind of python code and in order to check if they have items in their albums we'll just say if all albums. So this is going to be true as long as they have at least one album. So if they have albums then we can go ahead and display this code. And now to make an else statement else what are we going to display if they don't have any albums? I'll just say uh put like heading three no or say you don't have any albums. And actually whenever you have if else you need to end it with a end if. And that's just so D Jango knows where it's supposed to stop analyzing. So end if and there you go. So whenever we look at this since we do have albums it should be the same but if ever anyone was looking at this and they didn't then they would see this little indicator and that way they just don't have a blank screen and is like uh supposed to see something here or not what's going on.
Conclusion and Next Steps
Alright, so there you go. That is basically how you use a template, how your front end developer can give you, you know, the core blueprint of a web page and you can go in and pretty much plug in all the information that you need. And again, if it's kind of overwhelming, if we do like one other one then it's going to stick in your brain easy peasy. So thank you guys for watching. I will smell you next time.