Introduction to Django Templates
Django is a high-level Python web framework that allows rapid development and clean, pragmatic design. One of the key features of Django is its template system, which allows developers to define the structure and presentation of their web pages in a readable and reusable manner.
In this tutorial, we will be introducing Django Templates, discussing its syntax, and demonstrating how you can use them to create dynamic web pages.
What is a Django Template?
In Django, a template is a text file that defines the structure or layout of a file (such as an HTML file), with placeholders for actual content. These placeholders are replaced with real content when the template is rendered.
Django’s template system is designed to separate the presentation of data from the logic of your application. This is beneficial for a number of reasons:
- It allows designers to work on the presentation of a web page separately from developers working on the logic.
- It makes your code more readable and manageable.
- It allows you to reuse templates, reducing the amount of code you have to write.
Django Template Language (DTL)
Django comes with its own template language known as Django Template Language (DTL). DTL is designed to be easy to learn, with a minimum of coding knowledge. It's not as powerful as Python, but it's sufficient for most template use cases.
DTL uses tags, filters and variables:
- Tags are surrounded by
{% %}
and are used for logic in the template. - Filters are used to modify variables and are denoted by
|
. - Variables are surrounded by
{{ }}
and are used to display content.
Here's a simple example of a Django template:
<html>
<head>
<title>My first Django template</title>
</head>
<body>
<h1>Welcome, {{ user.name }}!</h1>
<p>Today's date is {% now "jS F Y" %}.</p>
</body>
</html>
In this example, {{ user.name }}
is a variable that will display the name of the user, and {% now "jS F Y" %}
is a tag that will display the current date.
How to Use Django Templates
To use Django templates, follow these steps:
Create a templates directory: Django will search for templates in a directory named "templates" in each installed application.
Create a template file: This is a text file that includes the structure or layout of your web page, with placeholders for the actual content.
Render the template: This involves loading the template into memory, filling in the placeholders with actual content, and returning the resulting string. You can render a template in a view using Django's
render()
function.
Here's an example of how to render a template:
from django.shortcuts import render
def welcome(request):
user = {'name': 'John'}
return render(request, 'welcome.html', {'user': user})
In this example, welcome.html
is the template file, and {'user': user}
is the context, which is a dictionary mapping template variable names to Python objects.
Summary
Django's template system is a powerful tool that allows you to separate the presentation of data from the logic of your application, making your code more readable and manageable. In this tutorial, we have introduced the basics of Django templates, including the syntax of Django Template Language and how to use templates in your Django applications. As you continue to learn Django, you will find that the template system is a key feature that greatly simplifies the development of complex web applications.