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:
- It’s easy to change the parent of a page without having to change the name of the content blocks.
- 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:
- Make the sidebar a partial and include it in the templates you need.
- 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:

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.
No, you can’t. From the Django docs on template inheritance:
I’m not clear on why you want to do this.
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.Anyone maintaining your code will quickly lose track of which
contentblock 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:
This lets you make the duplicated markup the default, and you can override it in those places where needed.
This may be helpful, too:
From what I can tell, this is probably your best choice:
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:
general.css:
Then your pages just look like, in the order of your examples:
plain_old_page.html:
page_with_left_sidebar.html:
page_with_left_sidebar_and_banner.html:
page_with_right_sidebar.html: