Templates
Django Templates is a system for rendering dynamic HTML content in a Django web application. It is one of the most important components of Django, as it enables the creation of user-friendly and aesthetically pleasing HTML pages.
How to use Django templates:
Setting up templates
In your settings.py
file, add the following line to specify the location of your templates:
TEMPLATES = [
{
...
'APP_DIRS': True,
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
},
]
This line tells Django to look for templates in a directory named templates
in the base directory of your project.
Creating templates
To create a template, create a new directory named templates
in the base directory of your project and add an HTML file.
Rendering templates
In your views.py
file, you need to render the template you created. You can do this by using the render
function from the django.shortcuts
module.
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'index.html')
In the above code, the render
function takes two arguments: the first is the request
object, and the second is the name of the template you want to render
.
Variables in templates
To pass data from your views to templates, you can use variables. For example:
def index(request):
context = {'variable_name': 'value'}
return render(request, 'index.html', context)
In the above code, we are passing a dictionary as the third argument to the render
function. The keys of the dictionary are the names of the variables, and the values are the values you want to pass to the template.
Template syntax
In a Django template, you can use the following syntax to access variables:
{{ variable_name }}
This syntax is used to access the value of a variable and print it on the template.
Template tags
Django has several built-in template tags that can be used to perform various operations in the templates. For example, you can use the for
tag to loop over a list of items and display each item:
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
You can read more about template tags in the django official docs
Template inheritance
Django templates allow you to inherit from other templates and include common elements across multiple pages in your application. This is called template inheritance.
Let's say you have a common header and footer that you want to include on every page of your website. Instead of repeating the header and footer code on every page, you can define the header and footer once and inherit from them on each page.
To use template inheritance, you need to create a base template that defines the common elements, such as the header and footer. In your base template, you use the block tag to define areas where child templates can add their own content.
Here is an example of a base template:
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<header>
<h1>Header</h1>
</header>
<main>
{% block content %}{% endblock %}
</main>
<footer>
<p>Footer</p>
</footer>
</body>
</html>
Then, in your child templates, you extend the base template and fill in the content for the blocks defined in the base template.
Here is an example of a child template:
{% extends 'base.html' %}
{% block title %}Page Title{% endblock %}
{% block content %}
<p>Welcome to my website!</p>
{% endblock %}
In this example, the child template extends the base template and fills in the content for the title
and content
blocks defined in the base template.
In your views, you can use the render
method to render a template and pass context data to the template. The context data is then available in the template for use.
Here is an example of using the render
method to render a template in a view:
from django.shortcuts import render
def index(request):
return render(request, 'index.html', {})
In this example, the render method is used to render the index.html template and pass an empty context dictionary to the template.
This is just a high-level overview of template inheritance in Django. You can learn more about Django templates and how to use them effectively in the Django documentation.