Creating and Using Templates
Introduction
Django, a powerful Python web framework, provides a versatile templating system known as Django Templates. In this tutorial, we will learn how to create and use these templates, which are a fundamental aspect of Django's Model-View-Template (MVT) design pattern.
What Are Django Templates?
Django Templates form the presentation layer in Django's MVT architecture. They are a text file that defines the structure or layout of a file (like HTML), with placeholders for actual content. These placeholders will be replaced with real data when the template is rendered. Django templating engine offers a user-friendly syntax and a variety of built-in tags and filters to handle many common scenarios.
Creating Django Templates
Let's start by creating a Django template. The convention is to place all your application's templates in a directory named templates
inside your application directory.
- Create a directory named
templates
in your application directory. - Inside the
templates
directory, create another directory with the same name as your application. For this tutorial, let's say our app is namedapp1
, create a directory namedapp1
. - Inside
app1
, create a file namedindex.html
. This is our Django template.
Here is the directory structure for clarity:
app1/
__init__.py
views.py
models.py
templates/
app1/
index.html
Let's add some HTML to our index.html
:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Django</title>
</head>
<body>
<h1>Hello, Django!</h1>
</body>
</html>
Using Django Templates
Now, we have our Django template ready. The next step is to use this template in a view.
Open views.py
and modify it as follows:
from django.shortcuts import render
def index(request):
return render(request, 'app1/index.html')
In the above code, we're importing the render
function from django.shortcuts
. This function takes a request
object and the path to a template file (relative to the templates
directory), and returns an HttpResponse
object with that rendered text.
Template Variables
Django templates are more than just a static HTML file. You can use variables, which get replaced with values when the template is rendered.
Let's modify our index.html
to use a variable:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Django</title>
<head>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>
Here, {{ name }}
is a template variable. We can pass the value for this variable from our view. Let's modify the index
view:
def index(request):
context = {'name': 'Django Learner'}
return render(request, 'app1/index.html', context)
The render
function takes a third argument, context
, which is a dictionary that maps template variable names to Python objects.
Conclusion
That's it! You have successfully created and used a Django template. Django Templates are powerful and flexible, they allow you to separate your presentation logic (HTML, CSS) from your business logic (Python). This is just the beginning, Django's templating engine offers many more features like template inheritance, custom tags, and filters, which we will explore in future tutorials. Happy learning!