问题
I would like to have some advice in constructing django template levels.
Reading the docs, I do not understand how to create a template hierarchy structure with more than 2 levels.
For example, I would like to create 3 levels of inheritance:
base.html
└── base_level2.html
├── level2_level3_1.html
└── level2_level3_2.html
This is my code:
base.html
First level {% block level2 %}{% endblock %}base_level2.html
{% extends "base.html" %} {% block level2 %} Second level {% block level3_1 %}{% endblock %} {% block level3_2 %}{% endblock %} {% endblock %}level2_level3_1.html
{% extends "base_level2.html" %} {% block level3_1 %} Third level, part 1 {% endblock %}level2_level3_2.html
{% extends "base_level2.html" %} {% block level3_2 %} Third level, part 2 {% endblock %}views.py:
def myView(request): return render_to_response('level2_level3_1.html', {}, context_instance=RequestContext(request))
In this way I can see the following on my browser:
First level
Second level
Third level, part 1
And this is logical to me because I call render_to response only on level2_level3_1.html.
Of course, if call level2_level3_2.html, I get the message Third level, part 2 but not the Third level, part1.
How to solve this? Is that a good approach? I've structured stuff in this way because my real templates are very big, a lot of lines of code, so I would like to keep some order. Any advice will be appreciated.
回答1:
It's hard to say if it's a good or bad idea or not without knowing the specific functionality of your templates, but my immediate reaction is that you're trying to over organize your templates. I think most people would urge you away from more than a 3-tier system because it makes it more difficult to make small changes in the website and more difficult to keep track of where things are. from the Zen of Python:
Flat is Better than Nested
The recommendation for a 3-tier system inTwo Scoops of Django goes like this:
- Each app has a
base_<app_name>.htmltemplate. App-level base templates share a common parent, base.html.- Templates within apps share a common parent base_
<app_name>.html template.- Any template at the same level as base.html inherits base.html
and for your naming schema, it might look like this:
| Templates/
|--base.html
|--someothertemplate.html # extends base.html
|--level2/
|----base_level2.html # extends base.html
|----level2_1.html # extends base_level2.html
|----level2_2.html # extends base_level3.html
EDIT: and there's no real reason for this:
Second level
{% block level3_1 %}{% endblock %}
{% block level3_2 %}{% endblock %}
where each block refers to the content of one template. you can simplify that to one block like
{% block level3 %}{% endblock level3%}
and then in each of the level3 templates, rename the blocks accordingly
回答2:
Probably not the best way of doing it but you might user include https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include
something like this for base_level2.html
{% extends "base.html" %}
{% block level2 %}
Second level
{% include "level2_level3_1.html" %}
{% include "level2_level3_2.html" %}
{% endblock %}
i've not tested this, so not sure it works.
and btw:
The include tag should be considered as an implementation of “render this subtemplate and include the HTML”, not as “parse this subtemplate and include its contents as if it were part of the parent”. This means that there is no shared state between included templates – each include is a completely independent rendering process.
来源:https://stackoverflow.com/questions/16899917/django-template-inheritance-how-many-levels-and-what-page-to-render