Reputation: 13
I'm writing an application in which user can choose one of several tools for data analysis and open it in a panel on main page. Is it possible to use django "extends" and have each tool defined in different file?
The minimal example of what im strugling with would be like this:
base.html
<div>
{% block left_panel %}
left block
{% endblock content%}
</div>
<div>
{% block right_panel %}
right block
{% endblock %}
</div>
and sample left_panel and right_panel tools:
left1.html
{% extends "base.html" %}
{% block left_panel %}
<p>TEST left 1</p>
{% endblock %}
right1.html
{% extends "base.html" %}
{% block right_panel %}
<p>TEST right 1</p>
{% endblock %}
Is there a way to render the base.html with both blocks overwriten?
Upvotes: 1
Views: 5717
Reputation: 15104
I believe that the best way to implement your requirement is to create a new template that extends base.html and includes left1.html and right1.html. Something like this:
{% extends "base.html" %} {% block left_panel %} {% include "left1.html" %} {% endblock content%} {% block right_panel %} {% include "right1.html" %} {% endblock %}
Update based on OP's comment: Actually you just need one configurable template, not 100. Let's say that based on the tools the user selects, your view passes the left_tool
and right_tool
context variables to your template. Now, you can easily do something like this:
{% block left_panel %} {% if left_tool == "tool1" %} {% include "left1.html" %} {% elif left_tool == "tool2" %}} {% include "left2.html" %} etc ... {% else %} {% include "left10.html" %} {% endif %} {% endblock content%}
You'll do the same with the right panel. Of course the above is a little naive and definitely not DRY -- instead you could for instance generate the name of the template to be included in the view and pass it directly to the template, or use a custom node etc.
Upvotes: 8