rebelliard
rebelliard

Reputation: 9611

Access django context variable from parent template

I can't set a django template variable from inside a children, any ideas what's wrong?

In my views.py:

return { 'header_title' : 'my text' }

base.html:

{{ header_title }}

main.html:

{{ extends "base.html" }}

details.html:

{{ extends "main.html" }}

That is not working. Help?

Upvotes: 1

Views: 1208

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239290

You can't just return a dict from a view. A view must return an HttpResponse object. Try instead:

Django 1.3+ render

return render(request, 'main.html', { 'header_title': 'my text' })

Django <1.3 render-to-response

return render_to_response('main.html', { 'header_title': 'my_text' }, context_instance=RequestContext(request))

Upvotes: 1

K&#246;ver
K&#246;ver

Reputation: 381

A solution could be to put in your base.html

<block title><endblock>

and in the chilren templates:

<block title>{{ header title}}<endblock>

Upvotes: 0

Related Questions