📄️ Introduction to the Template System
In the previous chapter we saw how to construct HTML code from a view and return it as the content of a response via the django.http.HttpResponse class. However, this procedure of including HTML code within Python code becomes problematic when you start working with real web applications, where HTML code is often voluminous and some of it is repeated on many pages. Furthermore, it is not a good practice to mix two very different concepts of an application: the logical component (Python code) and the graphical component (HTML).
📄️ Context And Variables
In a Django template you can use variables defined in your Python code. To do so, include these variables in a dictionary that is passed as the first argument to the render(). This dictionary is known as context. So, the context defines the variables that you want to be available in a template. For example:
📄️ Conditional Statements
Django's template system provides conditional statements, which fulfill the same objective as in Python code, but in this case whether or not the condition is met determines whether a block of code from the template is displayed or not. For example:
📄️ Loops
In a template you can execute the equivalent of Python's for loop, which is ideal for building HTML lists or tables from a Python collection, as we had done in the previous chapter from the courses() view. For example:
📄️ URLs
If you remember the urls.py file of our application, you will note that each URL is identified with a name given as the third argument to the path() function:
📄️ Static Files
JavaScript and CSS files, images, PDF documents, etc., are known as static files in web development. In general, any file whose content is not generated in real time in each request is a static file. There are several reasons why these files receive special treatment. One of them is that, once a Django project is set up in production, you will want the static files to be served directly by the web server, not by the framework. We will look at this in more detail in the last chapter. To make this possible, you will have to use a special tag in the templates every time we refer to a static file.
📄️ Extending Templates
You will much likely have repeated code between the different templates of your application. For example, the header and footer of a website usually appear on all pages. To avoid having the same blocks of code on each page, Django provides a template inheritance feature.
📄️ Addendum: Pattern Matching in URLs
In previous sections, we have created the /courses URL and bound it to the courses() view, which displayed a list of courses stored in courses.db. Now suppose that you want to create a course() view where the details of a particular course are displayed. For this, you could configure a /course URL followed by the name of the course from which you want to return the information, for example, /course/Python, /course/Java, /course/PHP, etc. Let's do it so by adding that configuration to our myapp/urls.py file: