[Django]-Django, how to use render_to_response with many templates

6👍

Option 1) Try using the {% include %} tag.


main.html

<head> ... </head>
<body>
{% block content %}
{% endblock content %}

template1.html

{% extends "main.html" %}
{% block content %}
     <h1>Hello world</h1>
     {% include "nested_template2.html" %}
{% endblock content %}

nested_template2.html

<p>The world is smaller than you think.</p>

In your view/controller:

return render_to_response('template1.html', {

Option 2) Chain {% extends ... %} tags as deep as you want. I frquently use this structure:

templates/
----base.html
----projects/
    ----_.html
    ----detail.html
    ----list.html

The base.html is the master layout. The folder/_.html is specific to a “stage” (more modular content).


base.html

<head> ... </head>
<body>
{% block stage_content %}
{% endblock stage_content %}

projects/_.html

{% extends "main.html" %}
{% block stage_content %}
     <h1>Project Stage</h1>
     {% block page_content %}
     {% endblock page_content %}
{% endblock stage_content %}

projects/list.html

{% extends "projects/_.html" %}

{% block page_content %}
   {% for project in projects %}
       <li>{{ project.name }}</li>
   {% endfor %}
{% endblock page_content %}

projects/detail.html

{% extends "projects/_.html" %}

{% block page_content %}
   Viewing project {{ project.name }}
{% endblock page_content %}

In your view/controller:

return render_to_response('projects/detail.html', {

Leave a comment