Include Other Templates

A snippet showing how to include templates inside other templates.


# models.py

from django.db import models

class MenuLink(models.Model):
    label = models.CharField(max_length=50)
    url = models.CharField(max_length=200)

    def __str__(self):
        return self.label
      

# templates/partials/_nav.html
<nav>
  <ul class="nav">
    {% for link in links %}
      <li class="nav-item"><a href="{{ link.url }}" class="nav-link">{{ link.label }}</a></li>
    {% endfor %}
  </ul>
</nav>

# templates/base.html

<body>
  {% include "partials/_nav.html" with links=menu_links %}
  {% block content %}{% endblock %}
</body>
      
Explanation:
  • Split large templates into partials and include them with {% include %}.
  • Pass context using with; the included template gets its own scope.
  • Great for headers, footers, and repeated UI chunks.
  • Category Templates
  • Total Views 767
  • Last Modified 30 January, 2026
  • Tags #templates #include #reuse #snippets
Previous snippet
If Statement in Templates
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.