Template inheritance and passing variables

1,075 views
Skip to first unread message

bnkwsk

unread,
Apr 10, 2012, 5:36:52 PM4/10/12
to Django users
Hi,
while writing my first website in Django I ran into troubles with
passing variables in my inherited templates. Let me describe my
templates' structure.

I have a base template for all applications' templates which includes
some parts - just to divide the code into smaller parts.

My goal is to pass a variable (which will be different for every view
in every app) to a template included in base template (which is
inherited by my app's view's templates). I considered using a custom
context processor but since every view will have its own value of the
variable, it seems to be a bad idea.

-- base.html ------------------------------------------------------
<!DOCTYPE html>
<html lang="pl">
{% include "base_head.html" %}

<body>
{% include "base_login.html" %}

{% include "base_header.html" %}

{% include "base_menu.html" %}

{% block content %}
<p>Default content...</p>
{% endblock %}

{% include "base_footer.html" %}
</body>
</html>
-------------------------------------------------------------------

-- base_footer.html -----------------------------------------------
{{ app_variable }}

<div class="container_12 footer-links">
<div class="grid_12">
<p>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
</p>
</div>
</div>
-------------------------------------------------------------------

Now, in one of my app's view I set the app_variable and render the
app's template.

-- views.py -------------------------------------------------------
def read(request, news_id):
news = get_object_or_404(News, pk = news_id)

app_variable = 'foo'

return render_to_response('news_read.html', { 'news': news,
'app_variable': app_variable }, context_instance =
RequestContext(request))
-------------------------------------------------------------------

-- news_read.html -------------------------------------------------
{% extends "base.html" %}

{% block content %}
<div class="container_12">
<h2>{{ news.title }}</h2>

{{ news.introduction|safe }}

{{ news.content|safe }}
</div>
{% endblock %}}
-------------------------------------------------------------------

Now, what should I do in order to have the app_variable in base_footer
template's context?
I'd be grateful for your help and advice.

andrea mucci

unread,
Apr 10, 2012, 6:46:52 PM4/10/12
to django...@googlegroups.com
Hi
in django documentation 

Note
The include tag should be considered as an implementation of "render this subtemplate and include the HTML", not as "parse this subtemplate and include its contents as if it were part of the parent". This means that there is no shared state between included templates -- each include is a completely independent rendering process.


so a good solution will be, for example


-- base.html ------------------------------------------------------
<!DOCTYPE html>
<html lang="pl">


<body>


{% block content %}
<p>Default content...</p>
{% endblock %}


{% block footer %}

{{ app_variable }}

<div class="container_12 footer-links">
<div class="grid_12">
<p>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
</p>
</div>
</div>
{% endblock %}
</body>
</html>


with this simple code you can use app_variable and in the same time if you want to change the footer code you can call from your template view the footer block and add different html code

cheers
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Reply all
Reply to author
Forward
0 new messages