The include statement renders another template and outputs the result into the current template. The included template has access to context of the current template by default.

 

#home/views.py
from django.shortcuts import render

# Create your views here.
def index(request):
    kdict = {
        'siteTitle':'Khmer Web',
        'pageTitle': 'Home',
        'message': '<b>ស្វាគមន៍​មក​កាន់​ទំព័រ​ដើម!</b>'
    }
    
    return render(request,'base.html',context={'data':kdict})

 

<!--home/templates/header.html-->
{% load static %}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <title>{{ data.siteTitle }} | {{ data.pageTitle }}</title> 
        <link rel="stylesheet" href="{% static 'styles/base.css' %}" />
        <link rel="stylesheet" href="{% static 'fonts/setup.css' %}" />
        <link rel="icon" href="{% static 'images/siteLogo.png' %}" />
    </head>
    <body>

 

<!--home/templates/body.html-->
<p>
    {% for key,value in data.items %}
        <p>{{ value|safe }}</p>
    {% endfor %}
</p>

 

<!--home/templates/footer.html-->
    </body>
</html>

 

{% include 'header.html' %}
{% include 'body.html' %}
{% include 'footer.html' %}

 

GitHub: https://github.com/Sokhavuth/khmerweb-django

Heroku: https://khmerweb-django.herokuapp.com