Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7597575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:11:22+00:00 2026-05-30T22:11:22+00:00

What is the Django way of creating a reusable template? Example: Suppose a lot

  • 0

What is the Django way of creating a reusable template?

Example: Suppose a lot of my pages contain a “Latest News” box and following the DRY principle, I would like to define it once and reuse it in the other pages. How would I do this with Django (or Jinja2) templates?

Reading through Django’s Template Documentation I get the impression that Django templates offer “top-down” inheritance where the sub-template itself determines in which super-template it is going to be embedded:

<!-- Super-template (not valid, for illustration): -->
<html>
  <head><title>Title</title></head>
  <body>{% block content %}{% endblock %}</body>
</html>
<!-- Sub-template: -->
{% extends "base.html" %}
{% block content %}
<div class="latest-news">News</div>
{% endblock %}

So what is the technique to reuse a block (a sub-template) in several places?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:11:24+00:00Added an answer on May 30, 2026 at 10:11 pm

    The most flexible way to reuse template fragments is to define an inclusion_tag. You can pass arguments to your custom tag, process them a bit in Python, then bounce back to a template. Direct inclusion only works for fragments that don’t depend on the surrounding context.

    Quick example from the docs:

    In app/templatetags/poll_extras.py register the tag with a decoration:

    from django import template
    register = template.Library()
    
    @register.inclusion_tag('results.html')
    def show_results(poll):
        choices = poll.choice_set.all()
        return {'choices': choices}
    

    In app/templates/results.html:

    <ul>
    {% for choice in choices %}
        <li> {{ choice }} </li>
    {% endfor %}
    </ul>
    

    Calling the tag:

    {% load poll_extras %}
    {% show_results poll %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Django documentation states the following clearly: Not every template in contrib\admin\templates\admin may be
I am essentially creating a blog application in django as a way of learning
I have a query that I'm trying to figure the django way of doing
Given a url to an image is there a way in Django/Python to pull
Before Django 1.0 there was an easy way to get the admin url of
In Django I'm looking for a way to serve several different files at once.
Using django, which is the way to check if data is present? I know
Is there a recommended way of using Django to clean an input string without
Is there a way to force django to display nothing instead of None for
Is there any way to group the models in django admin interface? I currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.