🧩 Django Include – Reuse Common Template Blocks Across Pages (2025 Guide)
🧲 Introduction – What Is {% include %} in Django?
In Django templates, the {% include %} tag lets you embed one template inside another, making it easy to reuse common UI components like headers, footers, navbars, alerts, or any repeatable HTML blocks. It keeps your code clean, modular, and DRY (Don’t Repeat Yourself).
🎯 In this guide, you’ll learn:
- How to use
{% include %}to inject templates - Where to place included files
- How to pass variables into included templates
- Best practices for maintainable UI code
🧱 Basic Syntax
{% include "partials/header.html" %}
✅ Renders the contents of header.html exactly where it’s placed.
📁 Folder Structure Example
templates/
├── base.html
├── home.html
└── partials/
├── header.html
├── footer.html
└── sidebar.html
🧩 Example: Include Header & Footer
base.html
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}My Site{% endblock %}</title>
</head>
<body>
{% include "partials/header.html" %}
<main>
{% block content %}{% endblock %}
</main>
{% include "partials/footer.html" %}
</body>
</html>
🔄 Passing Variables to Included Templates
By default, included templates have access to the parent’s context:
{% include "partials/user_card.html" %}
To pass a specific context only:
{% include "partials/user_card.html" with user=user %}
Or multiple values:
{% include "partials/alert.html" with message="Saved!" type="success" %}
🧪 Optional: Handle Missing Templates Gracefully
Avoid breaking the page when the file doesn’t exist:
{% include "partials/optional_nav.html" ignore missing %}
✅ This skips rendering if the template is missing, without error.
✅ Best Practices
- ✅ Use
{% include %}for partials like headers, navbars, cards, messages - ✅ Organize includes in a
partials/orincludes/folder - ✅ Use
{% include %} with ...for scoped and explicit variables - ✅ Keep included templates logic-free when possible
📌 Summary – Recap & Next Steps
🔍 Key Takeaways:
{% include %}injects reusable template code into other templates- Helps you write DRY, modular, and maintainable HTML
- Supports variable passing and error-tolerant includes
⚙️ Real-World Relevance:
Ideal for component-based template structure—blogs, admin panels, dashboards, and multi-page websites benefit from include for layout consistency.
❓ Frequently Asked Questions (FAQ)
❓ What’s the difference between {% include %} and {% extends %}?
✅ {% extends %} is for inheritance;
✅ {% include %} is for composition (reuse small blocks).
❓ Can I include multiple templates conditionally?
✅ Yes. Use:
{% if user.is_authenticated %}
{% include "partials/user_nav.html" %}
{% else %}
{% include "partials/guest_nav.html" %}
{% endif %}
❓ Can included templates use block tags?
🚫 No. Use block only in templates extended via {% extends %}.
❓ Will an include have access to all parent variables?
✅ Yes—unless you override with with or use {% include %} only to restrict access.
❓ Can I loop through includes?
✅ Yes. Example:
{% for product in products %}
{% include "partials/product_card.html" with product=product %}
{% endfor %}
Share Now :
