Зміст курсу
Django: Build Your First Website
Django: Build Your First Website
Jinja
Jinja is a powerful template engine for Python commonly used in web development, particularly in the Django framework. Two key concepts in Jinja that aid in creating efficient and easily extendable templates are blocks and extensions.
Blocks
Blocks are a mechanism that allows you to define areas in a template for dynamic content that can be overridden in child templates. The Jinja syntax for defining a block looks like this:
For instance, we can define a block for the page title:
This block can serve as a placeholder for inserting the page title, with "My Site" being the default value.
Template Extensions
Extensions enable the creation of a base template and child templates that can extend or override blocks from the base template. The key is to use the {% extends 'template' %}
construct to specify which template is being extended.
For example:
In this case, the template extends 'base.html', which might contain a common layout for all pages on the website.
Loops
Loops are constructs that allow you to iterate over data structures such as lists or dictionaries, outputting template content for each item.
Here are the key elements of the loop syntax in Jinja:
In this example, items is an iterable object, like a list, and for each iteration of the loop, item is output.
In the next chapter, you'll see a clear demonstration of Jinja's usage in our project.
Дякуємо за ваш відгук!