Locked lesson.
About this lesson
In this lesson, we'll discuss how Tags work and how to use them for logic and list functions within your Django code, e.g. for, if, else.
Exercise files
Download this lesson’s related exercise files.
Tags (for, if, else).docx57.3 KB Tags (for, if, else) - Solution.docx
58.1 KB
Quick reference
Tags (for, if, else)
Tags allow us to use if/else statements and for loops on a web page using the Django Template Language.
When to use
Use them whenever you need to do some logic (if/else statements) or loop through data on a web page.
Instructions
To use an if/else tag:
{% if variable == "something" %}
do something
{% else %}
do something else
{% endif %}
To use a for loop:
{% for name in names %}
print out: {{ name }} <br/>
{% endfor %}
Hints & tips
- Tags allow us to do if/else statements and for loops on a web page.
- Don't forget to close your tags!
- 00:04 Okay, in this video I want to talk about tags and tags are sort of the last thing
- 00:07 in Django template language that we're going to look at.
- 00:10 So I'm back at the same documentation page that we've been looking at in the last
- 00:14 few videos.
- 00:14 We can just come over here and click on Tags to skip down to the tags section.
- 00:18 And tags allow us to do certain Python programming type things on a web page.
- 00:25 So if you're familiar with Python or any programming language, the main basic
- 00:28 things you do with programming languages are you use variables.
- 00:32 We talked about those in the last couple of videos.
- 00:34 You also do logic, if else statements.
- 00:37 And you also loop through lists.
- 00:39 Those are sort of common programming language things.
- 00:42 And tags allow us to do all of those things on a webpage.
- 00:46 So specifically we're going to look at if else statements and for
- 00:50 loops that allow us to loop through things.
- 00:53 So if you're familiar with programming, you know what an if else statement is and
- 00:57 you know what a loop is.
- 00:58 So I'm just going to kind of go over this very quickly.
- 01:00 But to use a tag, we just use the same opening and closing brackets that
- 01:04 we've been using, and then call the thing that we want to do.
- 01:06 So if we want to do an if statement, we just call if.
- 01:09 If we want to do a for loop, we just call for.
- 01:11 So let's take a quick look at this in an example.
- 01:14 So let's go to our views.py file and
- 01:16 we have a first name of John that we're passing in through our context dictionary.
- 01:21 So let's do some logic on our about page.
- 01:24 So let's get rid of this stuff from the last couple of videos here,
- 01:28 clean this up a bit.
- 01:29 So I'm just going to create an if statement.
- 01:32 So let's say if first underscore name, which is that context variable,
- 01:36 equals John, and you'll notice I use double equal to signs,
- 01:40 that's what you always want to do in Python with an if else statement.
- 01:45 So if the first name is John, let's print out on the screen,
- 01:51 hello John, right, else let's print out hello stranger.
- 01:57 And with these tags, you always want to close them.
- 02:02 So to close and if tag for instance, we would end if,
- 02:06 if this was a four tag we would end for.
- 02:09 If this is a block, we end block.
- 02:11 Like right here, right, you see sort of the way this usually goes.
- 02:17 So end if and let's go ahead and save this.
- 02:22 Now let's head back over to our webpage and let's reload our about page and
- 02:25 it says, hello John.
- 02:27 And that's exactly what we would expect it to do.
- 02:30 So let's head back over to our views.py file and
- 02:33 let's change the first name to Bill.
- 02:35 So if we save this, then head back over to our about page and hit Reload.
- 02:40 It says, hello stranger.
- 02:41 Why?
- 02:42 Because the first name is no longer John.
- 02:44 If we pull up our code, it's saying if the first name is John say hello John,
- 02:50 else otherwise, print out hello stranger.
- 02:53 So, very cool.
- 02:55 And we could also use our variables inside of here.
- 02:59 So we could say, print out first underscore name.
- 03:03 So if we save this and come back and hit reload, it's going to say hello Bill.
- 03:07 So very, very cool and pretty simple.
- 03:09 So next, I want to show you very quickly a for loop.
- 03:12 So let's do this in our homepage.
- 03:14 So let's create a list, let's call it names.
- 03:17 And if you're familiar with Python lists,
- 03:20 you just use square brackets and then inside of here,
- 03:24 we just use parentheses, and we just make a list of whatever we want.
- 03:29 We want a list of names.
- 03:30 So let's go John, Steve, Bill, Mary, each one separated by a comma.
- 03:36 So now we can pass this names into our context dictionary
- 03:41 like we've done before and we want it to be names.
- 03:46 So now we can access this names on our webpage.
- 03:48 So in our homepage, let's create a quick for loop.
- 03:52 Let's get rid of this from the last couple of videos,
- 03:55 and let's put another line break to make it easier to see.
- 04:00 And to do a for tag, we just go for, and
- 04:02 now we need to create a variable name a dummy variable.
- 04:06 Well we're working with something called names plural.
- 04:10 So let's say name.
- 04:12 So for each name in names, we want to do something.
- 04:16 What do we want to do?
- 04:17 Well, let's just print out name, all right?
- 04:21 And let's put a line break so they're one on each line.
- 04:24 And finally, we need to close our tag.
- 04:28 So it's just end for because this is a for loop, so we need to end the for.
- 04:33 So let's go ahead and save this and head back to our homepage.
- 04:37 And what we do, we see John, Steve, Bill and
- 04:40 Mary printed out one at a time on a separate line.
- 04:43 And it's just looping through that whole list that we have, which is this list.
- 04:48 And it's just printing out the names under the screen.
- 04:52 So very cool, those are tags, those are for tags, and if else tags.
- 04:56 Very useful, you're going to use those for
- 04:59 all kinds of different things in Django, and pretty cool.
- 05:03 So in the next video,
- 05:04 I want to talk about using external Python programs in your Django code.
Lesson notes are only available for subscribers.