Including Templates with {% include %}
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.
<!-- 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.
<!-- 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.
<!-- 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. #}
- Confusing {% include %} with {% extends %} -- include pastes in a reusable fragment, extends inherits an entire page layout.
- Forgetting that an included template needs its own variables passed explicitly with with if it relies on names not in the parent's context.
- Overusing include for content that's actually page-specific, when a template block would be clearer.
- {% 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: