Django Tutorial #4: Understanding Templates in Django | Create Dynamic HTML Pages

Welcome back to my Django Tutorial Series! 🚀

In the previous tutorial, we learned about Django URLs and Views. Now it's time to understand another important component of Django: Templates.

Templates help developers create attractive web pages using HTML and display dynamic data received from Django views.

In this tutorial, we will learn:

  • What are Django Templates?
  • Why do we use templates?
  • How to create an HTML template.
  • How to connect templates with views.
  • Understanding Django Template Language (DTL).
  • Using variables, conditions, and loops.

1. What Are Django Templates?

Django Templates are HTML files that define how information is displayed to users.

They allow developers to separate the presentation layer from the application's business logic.

For example, instead of writing HTML directly inside Python views, we create separate HTML files and render them using Django.

2. Creating a Template Folder

Inside your Django application, create a folder named templates.

Example project structure:

myproject/ │ ├── myapp/ │ ├── views.py │ ├── urls.py │ └── templates/ │ └── home.html │ └── manage.py 

Create a file named home.html inside the templates folder.

3. Writing Your First HTML Template

home.html

<!DOCTYPE html> <html> <head> <title>My Django Website</title> </head> <body> <h1>Welcome to Django!</h1> <p>This is my first Django template.</p> </body> </html> 

4. Connecting Templates with Views

Now open views.py.

from django.shortcuts import render def home(request): return render(request, 'home.html') 

The render() function loads an HTML template and returns it as an HTTP response.

Now visit your home page in the browser to see the HTML content.

5. Understanding Django Template Language (DTL)

DTL stands for Django Template Language.

It allows us to display dynamic information and use basic programming logic inside HTML templates.

A. Variables

Variables display data passed from views.

Example:

def home(request): data = { 'name': 'Rahul', 'course': 'Python' } return render(request, 'home.html', data) 

In home.html:

<h1>Hello, {{ name }}!</h1> <p>Course: {{ course }}</p> 

Output:

Hello, Rahul!

Course: Python

B. If-Else Conditions

{% if marks >= 40 %} <p>Congratulations! You Passed.</p> {% else %} <p>Sorry! You Failed.</p> {% endif %} 

C. For Loops

{% for student in students %} <p>{{ student }}</p> {% endfor %} 

This displays each student from the list.

6. Important DTL Syntax

Syntax Purpose
{{ variable }} Displays a variable
{% if %} Checks a condition
{% else %} Alternative condition
{% endif %} Ends an if statement
{% for %} Starts a loop
{% endfor %} Ends a loop
{# comment #} Adds a template comment

7. Why Are Django Templates Important?

  • They separate HTML from Python code.
  • They make websites easier to maintain.
  • They allow dynamic content.
  • They support reusable layouts.
  • They help developers create organized applications.

8. Conclusion

Django Templates make it easier to build dynamic and user-friendly websites.

By combining HTML with Django Template Language, developers can display information, use conditions, and generate dynamic pages without writing HTML inside Python views.

This is another important step in my Django learning journey!

What's Next?

In Django Tutorial #5, we will explore Django Models and Databases and learn how to store and manage information in a Django application.

If you found this tutorial useful, share your thoughts in the comments!

Happy Coding! 💜

— SP Randhawa