97

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?

4 Answers 4

179

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>
Sign up to request clarification or add additional context in comments.

9 Comments

sorry, forgot to mention: the subtemplate might be instantiated several times into the basic_template.html
That is also possible, you can include a template in a for loop in the template, see the updated answer.
in the item loop, shouldn't i pass item in the subtemplate context, like {% include "includes/subtemplate.html" with item=item %}?
@barraponto you can do that but item is already available in the subtemplate. If you wish to keep the context of the sub template clean then you can use {% include "includes/subtemplate.html" with item=item only %} to only pass item from the parent to the subtemplate.
This should be added in the Django docs somewhere! Thanks for the helpful answer.
|
26

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

3 Comments

What's the difference between doing it this way and doing {% include "main/includes/example.html" %} ?
@MichaelLafayette, think of extend as inherit while include as import.
Are there any performance advantages involved in using one approach over the other? The {% include "main/includes/example.html" %} approach looks simpler (it affirms KISS).
2

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

Comments

1

I just wanted to add differences of extend and include. Both template and include can use models inserted in current app. Template is for global usage by your any app. Include is for use in certain apps. For ex: you want to insert Image Slider to your homepage and about page but nowhere else. You can create Slider app with its own model for convenience and import its model and include in that pages. If you used template for this example, you would create 2 templates one with slider and everything else other template have.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.