Reputation: 603
I am developing a website with django and want to keep a common template for header and footer. The contents of the header and footer vary with respect to the user logged in. So is there a way where i could use:
header=render_to_response('header.html',{....})
footer=render_to_response('footer.html',{....})
content=render_to_response('content.html',{....})
return header+content+footer
Upvotes: 1
Views: 1211
Reputation: 7587
Harsh, concatenating HttpResponse
objects is not the way to do it. Django's (and I believe, reasonable) approach recommends using templates including and inheritance. Please, take a look at {% block %}
, {% include %}
and {% extend %}
template tags here.
In your case the way to implement template inheritance looks like:
base.html :
<div> header code </div>
{% block content %}
<div>Default content</div>
{% endblock content %}
<div> footer code </div>
my.template.html :
{% extend 'base.html' %}
{% block content %}
<div>My new content</div>
{% endblock content %}
Upvotes: 4
Reputation: 55197
You should look into template inheritance.
Basically, this allows you to have a "Base" template with your header and footer, where you define (for instance) a "Content Block".
You then have your other templates "extend" the Base template by filling in the (empty or not) blocks you defined in your Base template.
Using render_to_string as you're doing is not the proper way of doing this in Django.
Upvotes: 1