How do you insert a template into another template?

There are mainly 2 ways (2 easy ones)

1:

In base html put {% include "myapp/sub.html" %}

And just write html code inside your sub.html file

2:

https://docs.djangoproject.com/en/dev/ref/templates/language/#template-inheritance


You can do this using a block. Blocks are a Django Template tag which will override sections of a template you extend. I've included an example below.

basic_template.html

<body>
{% block 'body' %}
{% endblock %}
</body>

template you want to include: (i.e. example.html)

{% extends 'basic_template.html' %} 
{% block 'body' %}
/* HTML goes here */
{% endblock %}

views.py:

return render_to_response(template='example.html', context, context_instance)

Doing this will load basic_template.html, but replace everything inside of {% block 'body' %} {% endblock %} in basic_template.html with whatever is contained within {% block 'body' %} {% endblock %}.

You can read more about blocks and template inheritance in the Django Docs


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>