Django: Getting Started Transcripts
Chapter: HTML Templates
Lecture: Chapter concepts
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Built into Django is a template rendering engine that lets you bring concepts you take for granted in your code to your output.
0:08
The original writers of Django worked at a newspaper in the city of Lawrence Kansas and built the first version to help them create web applications.
0:16
They had both programmers and HTML writers on their staff and didn't want the HTML folks to have to learn Python.
0:25
As such, they built a mini language used for composing HTML based on templates. The language is similar to the concepts in PHP.
0:33
You work directly in the file and then a series of special sequences allow you to control the contents. Django template language has two main ideas,
0:43
tags and filters, tags are actions and do things like looping and conditionals. Filters modify data before it is output. When you view a web page,
0:54
you're seeing the text output resulting from hitting a URL. That HTML document contains references to other things like images,
1:02
CSS files and javascript. Django refers to these other things as static files and provides
1:09
tools for referencing them and serving them from the development server. HTML tends to be rather repetitive.
1:16
It isn't uncommon for every file on a website to have the same header and footer
1:20
The template language embodies dry principles that's don't repeat yourself and allows you to inherit and compose pieces of HTML.
1:30
You can manage your output, like you manage your code. Django supports several different template engines.
1:36
It ships with both a Django specific one and support for jinja2. You can also plug in other template engines if you prefer.
1:45
In this chapter, I'll be focusing on the original Django templating mini language. The writers of Django made a design decision
1:52
to intentionally not expose Python inside of the HTML. This is to separate the roles of coder from HTML writer.
2:03
There are pros and cons to this decision. The pros are the mini language is much simpler and is designed to fail silently,
2:10
failing silently, makes debugging more difficult but it means mistakes aren't in the end users face.
2:16
The cons are you sometimes want to do something in the mini language that it wasn't made to do and you end up having to jump through hoops.
2:25
Of course, you always have the coding logic in your views to do fancier stuff.
2:30
To write in the mini language, you directly write the content and then use built in and third party tags to control the flow of the content,
2:39
including loops, conditional and re use and then you can apply filters to modify the data in place.
2:48
For example, a filter can take a raw date and format it into a pleasing fashion separating again the data from its presentation.
2:57
The template engine is usually used to render HTML but can handle any text format. I often use it myself to compose
3:04
multipart MIME email messages and if all this isn't great enough, the templates themselves get compiled before being rendered.
3:13
This happens behind the scenes so you don't have to worry about it but it means the rendering step can be pretty quick.