← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 7 of 10

Including Templates with {% include %}

{% include %} is like pasting a reusable sticker -- a small chunk of HTML, like a navbar, into as many pages as you want.

Including a Reusable Fragment

{% include %} renders a separate template file and drops its output directly into the current template at that spot.

Example: Including a Reusable Fragment

When home.html renders, Django inserts navbar.html's rendered HTML right where {% include %} appears.

markup
<!-- templates/navbar.html -->
<nav>
  <a href="/">Home</a>
  <a href="/about">About</a>
</nav>

<!-- templates/home.html -->
{% include "navbar.html" %}
<h1>Welcome!</h1>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Passing Extra Context with 'with'

The with clause lets you pass extra variables into the included template without changing the parent's context.

Example: Passing Extra Context with 'with'

label is only available inside card.html for this specific include -- it doesn't leak into the rest of the parent template.

markup
<!-- templates/card.html expects {{ label }} -->
{% include "card.html" with label="Featured Product" %}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

include vs extends

include pastes a reusable fragment at one spot in the page; extends inherits an entire base layout and lets you override named blocks within it.

Note: Use include for small repeated pieces (navbar, card, footer); use extends for whole-page structure.

Example: include vs extends

Both tags reuse templates, but include is for fragments while extends is for full-page inheritance.

markup
<!-- fragment reused on many pages -->
{% include "footer.html" %}

<!-- vs inheriting a full page layout -->
{% extends "base.html" %}
{% block content %}...{% endblock %}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Confusing {% include %} with {% extends %} -- include pastes in a reusable fragment, extends inherits an entire page layout.
  2. Forgetting that an included template needs its own variables passed explicitly with with if it relies on names not in the parent's context.
  3. Overusing include for content that's actually page-specific, when a template block would be clearer.
Chapter Summary
  • {% include 'template.html' %} inserts another template's rendered output at that point.
  • Included templates share the parent's context by default, or you can pass extra variables using with.
  • include is ideal for reusable fragments like navbars, footers, and cards used across many pages.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.