Chapter 6: Django include Tag

 include Tag: The {% include %} tag

Many beginners either:

  • copy-paste the same HTML block (navbar, footer, question card, pagination, alert messages…) into 5–10 different templates → nightmare when you want to change one thing later
  • or they overuse template inheritance ({% block %}) for things that are not really “replaceable sections” but “reusable snippets”

{% include %} solves exactly that second problem — it lets you pull in a small, independent template fragment anywhere you want.

Let’s learn it properly — step by step — like I’m sitting next to you, creating real reusable pieces together in your polls project.

1. The Simplest Possible {% include %}

Create a tiny reusable snippet:

File: polls/templates/polls/_vote_count_badge.html

HTML

Now use it in any template:

HTML

→ The badge appears exactly the same way everywhere — change it once, everywhere updates.

2. Why the Underscore _ Prefix?

It’s a very strong convention (used by almost all Django developers):

  • Files starting with _ are partials / fragments — not meant to be rendered alone
  • They are included, never directly used in views
  • Helps everyone instantly understand: “this is not a full page”

Common names:

  • _question_card.html
  • _pagination.html
  • _alert_message.html
  • _form_errors.html
  • _footer.html (even if you also use block inheritance)

3. Passing Context with with (Very Important)

Without with → the included template gets exactly the same context as the parent.

With with → you can rename or add variables:

HTML

The only keyword is very powerful:

  • only → included template gets only the variables you pass in with
  • No access to parent context (safer, cleaner, avoids bugs)

Real example — showing choices with question number:

HTML

And _choice_line.html:

HTML

→ Clean, no leakage of outer variables

4. Dynamic Include (Advanced but Very Useful)

You can include different partials based on condition:

HTML

Or even variable-based:

HTML

(though most teams avoid this — too magic)

5. {% include %} with with and Context Modifiers

HTML

→ Very clean way to reuse user avatar + name + role block everywhere

6. Common Real-Life Use Cases in 2026 Projects

  1. Reusable cards / list items

    _question_card.html → used in index, search results, dashboard

  2. Pagination snippet

    _pagination.html → used in every list view

  3. Form error/success messages

    _form_messages.html → include after every POST form

  4. Alert banners

    _alert.html → show site-wide announcements

  5. Loading spinner / skeleton loader

    _skeleton_loader.html → during AJAX loads

  6. Admin-style action buttons

    _admin_actions.html → edit/delete links (when shown on frontend)

7. Common Beginner Mistakes & Fixes

Mistake Symptom / Error Correct way
{% include “polls/_badge.html” %} → 404 Wrong path Path is relative to templates/ root → polls/_badge.html
Variables not available in included file Forgot with or only Use with var=… only when you want isolation
Included file shows parent variables unexpectedly No only keyword Add only to prevent context leakage
{% include %} before {% extends %} SyntaxError extends must be first tag
Circular includes (file A includes B, B includes A) Recursion error Avoid — refactor to common partial

Your Quick Practice Task (Do This Right Now)

  1. Create polls/templates/polls/_vote_count_badge.html (copy the snippet above)

  2. In index.html or detail.html, replace any vote display with:

    HTML
  3. Create another partial _question_meta.html:

    HTML

    Include it:

    HTML
  4. Try commenting one include with {% comment %} → see it disappear

Tell me what you want next:

  • Which part of {% include %} feels confusing? (with/only? path? naming?)
  • Want to see how to include from another app ({% include “blog/_post_card.html” %})
  • Ready to learn {% with %} tag (local variable scope)?
  • Or want more partial examples (pagination, alerts, form fields)?
  • Or finally ready for Django Forms + Voting + POST + F()?

You’re now writing clean, reusable, professional templates — this {% include %} habit alone will save you days of work later.

Keep going — you’re doing really well! 🚀🇮🇳

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *