Django: Getting Started Transcripts
Chapter: HTML Templates
Lecture: Common template tags
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
There are loads of tags and filters built into Django and you can also install third
0:05
party tag libraries. You can even write your own but that's beyond the scope of this course. Let me cover a few of the more common tags.
0:14
You saw the four tag in the previous example for each item in a container, the contents of the tag gets rendered.
0:21
The value of item can be referenced as a variable inside of the context of the
0:26
loop. There are also some predefined values that get you info about the loop. There is a counter this is like using enumerate in Python,
0:35
a bullion to indicate if this was the first and another one for the last pass through the loop reverse counters and others as well.
0:44
The four tag also supports an empty clause that works like an else condition. It only gets rendered if the loops container was empty.
0:53
This is particularly useful if you want to show a message like there are no books in your library instead of just an empty space.
1:01
This saves you wrapping your forloop with a giant if condition. Speaking of you also saw the if tag it works like an if statement in Python
1:10
Also like Python, it supports both an elif and an else clause. The comment tag comments out a block unlike an HTML comment,
1:21
this doesn't show up in the result. It removes the contents from the result entirely.
1:26
It has a neat little feature that you can pass in a string that it also ignores. This is handy if you're using the comment block to comment out a chunk
1:33
of the page when you're debugging, you can write a note to yourself as to why you did it. A comment within your comment. I haven't covered it yet,
1:43
but Django supports a mechanism for naming and referencing URLs This is good practice. It means you only have to define the actual URL
1:50
once and in all other places you use its name. That way, if you want to change it, you don't have to hunt down all the places you used it in your code.
2:00
As often as you want to use links in your HTML, The URL tag allows you to look up a named URL
2:06
It even supports constructing the parameters that get passed into the URL in both positional and keyword arguments.
2:14
You'll see more on this tag later in the course. What if you want to show content that uses the same symbols as the rendering engine
2:21
How do you escape them? Well, there's a tag for that. The verbatim tag renders the contents inside of it exactly as shown,
2:30
ignoring any brace brackets that would normally be interpreted by the rendering engine.
2:35
You can even name the verbatim block so that you can have verbatim tags inside of it.
2:40
The name is in the opening and closing tags are matched and everything inside even verbatim tags gets rendered.