I have a reusable HTML fragment that I use to list items. So to list items in a view I just do:
variables = RequestContext(request, {
'items': items,
}
return render_to_response('template_in_question',variables)
and the fragment is:
{% for item in items %}
<p>Item: {{item.name}} </p>
{% endfor %}
So far so good. However, there are views where I want to use the same reusable fragment twice. For example, if I want to list the most sold items and the latest items, I need to create two copies of that reusable fragment:
The view would be like this:
variables = RequestContext(request, {
'most_sold_items': most_sold_items,
'latest_items': latest_items
}
and in the HTML would need to have two reusable HTML templates:
{% for item in most_sold_items %}
<p>Item: {{item.name}}</p>
{% endfor %}
and a second one
{% for item in latest_items %}
<p>Item: {{item.name}}</p>
{% endfor %}
So my question is: How can I use, in the same view, two or more item lists, and use a common HTML template for that? For example, in the view above pass “most_sold_items” and “latest_items” and somehow use just one HTML template to list each separately?
You can do it with the include tag. Basically, you’d end up with: