I’m working on a Django site that has multiple sections and subsections. I’d like to have several depths of template inheritance: a base template for the whole site, one base template for each section that inherits from the root base template, and so on. Here’s a simplified version of my desired directory structure:
base.html section1/ base.html section2/ base.html section3/ base.html
What I would desire is for all the files under section1/ to contain something like {% extends 'base.html' %}, meaning they would extend section1/base.html. section1/base.html would contain something like {% extends '../base.html' %}, meaning that it would extend the root-level base file. However, I couldn’t find anything in the documentation suggesting this was possible, and I couldn’t get Django to distinguish between '../base.html' and 'base.html'. ({% extends '../base.html' %} throws an error.) I suppose one workaround would be to rename all base files base_SECTIONNAME.html, and update all the files that inherit from them, but I am concerned that this might become difficult to maintain as my site becomes bigger and sections change names, etc. I would prefer a solution that takes advantage of the natural hierarchy specified by directories and subdirectories.
Any ideas?
May be I oversee something, but all you want can be accomplished with the django template system. All extends calls are relative to template directories.
In order for all base.html files in subdirectories to extend base.html, you just have to put a
{% extends 'base.html' %}into the files. section1/base.html would would look like that.{% extends 'base.html' %}{# ... rest of your code ...#}Now, to get the files from section1 to extend section1/base.html you just have to put
{% extends 'section1/base.html' %}at the top of them. Same for section2, section3 and so on.It is just that simple, but might not totally obvious in the documentation.
I hope, I understood your question.