Django Templates: Learn how to create dynamic HTML templates using Django's template engine. Discover template tags, filters, template inheritance, and how to pass data to templates.
Django Templates are a powerful feature that allows developers to create dynamic HTML pages with ease. The Django template engine is designed to separate the presentation logic from the application logic. This allows for greater code reusability, maintainability, and flexibility in the development process.
In this article, we'll explore some of the key features of Django templates, including template tags, filters, template inheritance, and how to pass data to templates.
Template Tags
Django templates use a system of tags to allow for dynamic content to be inserted into HTML pages. These tags are enclosed in curly braces, and can be used to perform a variety of tasks such as iterating over lists, performing logical operations, and rendering variables.
For example, consider the following code:
```html
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}!</p>
{% else %}
<p>Please login to continue.</p>
{% endif %}
```
In this code, we use the `{% if %}` tag to check if the user is authenticated. If they are, we render a welcome message that includes their username. If they are not, we display a message asking them to log in.
Django provides many built-in tags that allow you to perform various tasks in templates, such as `{% for %}`, `{% if %}`, and `{% url %}`.
Template Filters
Filters are another important feature of Django templates. They allow you to modify the output of a variable before it is rendered in the template. For example, you might want to format a date in a specific way, or convert a string to uppercase.
Django provides many built-in filters, including `date`, `default`, `length`, and `upper`. Filters are used by chaining them onto a variable using the `|` character.
Here's an example of how you might use filters in a template:
```html
<p>{{ product.name|title }}</p>
<p>{{ product.description|truncatewords:20 }}</p>
```
In this code, we use the `|title` filter to capitalize the first letter of each word in the product name, and the `|truncatewords:20` filter to limit the product description to 20 words.
Template Inheritance
Template inheritance is a powerful feature that allows you to define a base template that can be reused across multiple pages. This can help to simplify your code, improve maintainability, and reduce code duplication.
To create a base template, you simply define the common elements of your pages, such as the header and footer. You can then create child templates that inherit from the base template and define their own unique content.
Here's an example of a base template:
```html
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}Default Title{% endblock %}</title>
</head>
<body>
<header>
<h1>My Site</h1>
</header>
<main>
{% block content %}
<p>No content defined.</p>
{% endblock %}
</main>
<footer>
<p>Copyright © 2023</p>
</footer>
</body>
</html>
```
In this code, we define a base template that includes a header, a main section with a default message, and a footer. We also use template tags to define blocks that can be overridden by child templates.
To create a child template, you simply extend the base template and override the desired blocks. Here's an example of a child template:
```html
{% extends "base.html" %}
{% block title %}Home{% endblock %}
{% block content %}
<h2>Welcome to my site!</h2>
<p>This
Comments
Post a Comment