bnkwsk
unread,Apr 10, 2012, 5:36:52 PM4/10/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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.