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 6044567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:01:35+00:00 2026-05-23T07:01:35+00:00

Is there a way to have content blocks with the same name? base.html: This

  • 0

Is there a way to have content blocks with the same name?

base.html:

This is the template with the main layout.

<html>

  ...

  {% block content %}

  {% endblock %}

  ...

</html>

base_side_left.html:

This is the template with the main layout + sidebar on the left.

{% extends 'base.html' %}

{% block content %}

  <div class='sidebar'>
  </div>

  {% block content %}

    //This doesn't work because you can't have blocks with the same name//

  {% endblock %}

{% endblock

I have a few reason why I am asking this:

  1. It’s easy to change the parent of a page without having to change the name of the content blocks.
  2. I don’t have to come up with names for my blocks. Like content-content, sidebar-content, etc

I got two solutions for this which I don’t like because they ain’t DRY:

  1. Make the sidebar a partial and include it in the templates you need.
  2. Add everything to the base template and overwrite those blocks you don’t need.

If this isn’t possible with Django Template can I do something like this with an other templating engine?

Small update:

template diagram

So what I want to do is to be able to move the templates around in the template tree without to much hassle. It’s not possible though without coming up with smart names for my content blocks but I thought I add this pretty diagram anyways.

  • 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-23T07:01:36+00:00Added an answer on May 23, 2026 at 7:01 am

    No, you can’t. From the Django docs on template inheritance:

    you can’t define multiple {% block %} tags with the same name in the same template. This limitation exists because a block tag works in “both” directions. That is, a block tag doesn’t just provide a hole to fill — it also defines the content that fills the hole in the parent. If there were two similarly-named {% block %} tags in a template, that template’s parent wouldn’t know which one of the blocks’ content to use.

    I’m not clear on why you want to do this.

    It’s easy to change the parent of a page without having to change the name of the content blocks.

    There’s only one {% block %} tag with a given name, and there’s only one {% extends %} tag as well. I don’t see any difference in difficulty.

    I don’t have to come up with names for my blocks. Like content-content, sidebar-content, etc

    Anyone maintaining your code will quickly lose track of which content block is effective and get confused. Besides, the names should have something to do with what the {% block %} is supposed to do. So I’m wondering why you’d have two templates, one including another, with blocks that are exactly the same.

    Another point from the docs:

    If you find yourself duplicating content in a number of templates, it probably means you should move that content to a {% block %} in a parent template.

    This lets you make the duplicated markup the default, and you can override it in those places where needed.

    This may be helpful, too:

    If you need to get the content of the block from the parent template, the {{ block.super }} variable will do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it. Data inserted using {{ block.super }} will not be automatically escaped (see the next section), since it was already escaped, if necessary, in the parent template.

    From what I can tell, this is probably your best choice:

    Make the sidebar a partial and include it in the templates you need.

    How is this not DRY? You have to repeat the {% include %} tag?

    I think there’s a better design solution that would work for you, but you haven’t given enough info on what you’re trying to accomplish for me to help further.

    EDIT: Looking at your example, you can do all that with a single template. CSS will take care of you here.

    page_template.html:

    <!DOCTYPE html PUBLIC -- ... -->
    <html>
    <head>
        <!-- ... other header fields ... -->
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <base href="{{ host }}{{ root }}{{ path }}" />
        <title>{% block title %}Untitled{% endblock %}</title>
        <link rel="icon" type="image/png"
            href="{{ root }}static/images/favicon.png" />
        <link rel="stylesheet" type="text/css"
            href="{{ root }}static/css/general.css" />
        <!-- other header fields here -->
    {% block head %}{% endblock %}
    </head>
    <body class="{% block page_class %}no_sidebar{% endblock %}">
        <div id="page_header">
            <!-- page heading here -->
        </div>
        <div id="sidebar">
            {% block sidebar %}<!-- default sidebar here -->{% endblock %}
        </div>
        <div id="banner">
            {% block banner %}{% endblock %}
        </div>
        <div id="content">
            {% block content %}{% endblock %}
        </div>
    </body>
    </html>
    

    general.css:

    body.no_sidebar div#sidebar,
    div#banner
    {
        display: none;
    }
    
    div#sidebar
    {
        width: 20%;
    }
    
    body.with_sidebar div#sidebar
    {
        float: left;
    }
    
    body.with_banner div#banner
    {
        display: block;
    }
    
    body.right_sidebar div#sidebar
    {
        float: right;
    }
    

    Then your pages just look like, in the order of your examples:

    plain_old_page.html:

    {% extends base.html %}
    
    {% block content %}
        <!-- content goes here -->
    {% endblock %}
    

    page_with_left_sidebar.html:

    {% extends base.html %}
    {% block page_class %}with_sidebar{% endblock %}
    
    {% block sidebar %}
        <!-- sidebar goes here, if different from default -->
        <!-- otherwise omit this section -->
    {% endblock %}
    
    {% block content %}
        <!-- content goes here -->
    {% endblock %}
    

    page_with_left_sidebar_and_banner.html:

    {% extends base.html %}
    {% block page_class %}with_sidebar with_banner{% endblock %}
    
    {% block sidebar %}
        <!-- sidebar goes here, if different from default -->
        <!-- otherwise omit this section -->
    {% endblock %}
    
    {% block banner %}
        <!-- banner goes here -->
    {% endblock %}
    
    {% block content %}
        <!-- content goes here -->
    {% endblock %}
    

    page_with_right_sidebar.html:

    {% extends base.html %}
    {% block page_class %}right_sidebar{% endblock %}
    
    {% block sidebar %}
        <!-- sidebar goes here, if different from default -->
        <!-- otherwise omit this section -->
    {% endblock %}
    
    {% block content %}
        <!-- content goes here -->
    {% endblock %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have the compile deduce the template parameter automatically? template<class
In the header region of my base template (main.html), I've placed an HTML5 media
I have a base template file (base.html) and every other template extends to it
Is there a particular way to make a <ul> a height? I have this
Is there any way to have something that looks just like a file on
Is there a way to have TortoiseSVN (or any other tool) auto-add any new
Is there a way to have a 64 bit enum in C++? Whilst refactoring
Is there a way to have Linux read ahead when cloning a disk? I
Is there a way to have a mutable static variable in F# class that
Is there a way to have Visual Studio 2008 automatically add heading information to

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.