Templates are an essential part of web development, allowing us to separate HTML structure from backend logic. In this tutorial, we will explore template engines used in Flask and Django:
A template engine helps in dynamically generating HTML content. Instead of manually inserting values in HTML files, we can use placeholders and control structures like loops and conditions.
Django comes with a built-in template engine, which automatically loads and renders templates.
In a Django project, templates are usually stored in a templates folder inside the app directory.
Example: Create an HTML file home.html in the templates/ folder
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Hello, {{ user_name }}!</h1>
<p>Today's date is: {{ current_date }}</p>
</body>
</html>In views.py, use the render() function to send data to the template.
from django.shortcuts import render
from datetime import datetime
def home(request):
context = {
'user_name': 'Rahul',
'current_date': datetime.now()
}
return render(request, 'home.html', context)Now, when you visit http://127.0.0.1:8000/, it will display:
Hello, Rahul!
Today's date is: [current date]
Django templates allow you to use template tags like loops and conditions.
{% if user_name %}
<h2>Welcome, {{ user_name }}!</h2>
{% else %}
<h2>Welcome, Guest!</h2>
{% endif %}<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>Flask uses Jinja2 as its default template engine, which is similar to Django Templates but more flexible.
Templates in Flask are stored inside a templates/ folder.
Example: Create templates/home.html
<!DOCTYPE html>
<html>
<head>
<title>Flask Template</title>
</head>
<body>
<h1>Welcome, {{ user_name }}!</h1>
<p>Today's date is: {{ current_date }}</p>
</body>
</html>In app.py, use render_template() to send data to the template.
from flask import Flask, render_template
from datetime import datetime
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html", user_name="Amit", current_date=datetime.now())
if __name__ == "__main__":
app.run(debug=True)Now, visit http://127.0.0.1:5000/, and you’ll see:
Welcome, Amit!
Today's date is: [current date]
{% if user_name %}
<h2>Welcome, {{ user_name }}!</h2>
{% else %}
<h2>Welcome, Guest!</h2>
{% endif %}<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>Jinja2 syntax is very similar to Django Templates, making it easy to switch between the two.
| Feature | Django Templates | Jinja2 (Flask) |
|---|---|---|
| Default in | Django | Flask |
| Flexibility | Limited (Security-focused) | More flexible |
| Performance | Optimized for Django | Faster in some cases |
| Best for | Full-stack Django apps | Microservices, APIs |
Django Templates are safer but have stricter rules.
Jinja2 is more flexible and commonly used outside Django.
This tutorial covered template engines in Django and Flask. Both use similar syntax with {% %} for logic and {{ }} for variables.
Sign in to join the discussion and post comments.
Sign inObject-Oriented Programming (OOP) in Python
Learn the fundamentals of Object-Oriented Programming (OOP) in Python, including classes, objects, inheritance, polymorphism, encapsulation, and more. Understand how OOP enhances code reusability, scalability, and organization.
Python Basics
Python is a powerful, high-level programming language known for its simplicity and versatility. It is widely used in various fields, including web development, data science, artificial intelligence, automation, and more. This tutorial series is designed to take you from the basics of Python to more advanced topics, ensuring a strong foundation in programming.