htmldjangodjango-templates

How do you insert a template into another template?


I have a very basic template (basic_template.html), and want to fill in the with data formatted using another partial template. The basic_template.html might contain several things formatted using the partial template.

How should I structure the code in views.py?

The reason I am doing this is that later on the will be filled using Ajax. Am I doing this right?


Solution

  • You can do:

    <div class="basic">
    {% include "main/includes/subtemplate.html" %}    
    </div>
    

    where subtemplate.html is another Django template. In this subtemplate.html you can put the HTML that would be obtained with Ajax.

    You can also include the template multiple times:

    <div class="basic">
    {% for item in items %}
        {% include "main/includes/subtemplate.html" %}    
    {% endfor %}
    </div>