Examples


Django Templates


  • In Django, templates are HTML files. A template has special syntax for inserting dynamic content from views.
  • Django templates have many controls like loops, conditionals, variables, filters and extend templates.

Create Django Template

Create a templates folder inside the myapp folder, and create a HTML file named index.html.

Open the index.html file and add the follwing code

myproject/myapp/templates/index.html
<!DOCTYPE html>
<html>
  <head>
    <title>{{title}}</title>
  </head>
  <body>
    <h1>{{message}}</h1>
  </body>
</html>
myproject/myapp/templates/views.py
from django.shortcuts import render

# Create your views here.
def index(request):
    data = {
        title : "Home Page",
        message : "Welcome to Django"
    }
    return render(request,"index.html",data)

In this example, {{ title }} and {{ message }} are template variables. These will be replaced by the context data passed from the Django view.

Output

Django templates folder index output

Prev Next