2

I have the following scenario:

2 views:

view1:

return render(request, 'template1.html', {'var1': 'value1'} )

view2:

return render(request, 'template2.html', {'var2': 'value2' } )

2 templates:

template1.html

{% block foo %}
{{ var1 }}
{% endblock %}

template2.html

{% extends template1.html %}
{% block foo %}
{{ block.super }}
{{ var2 }}
{% endblock %}

Desired Output of Template1.html:

value1

Real Output of Template1.html:

value1

Desired Outputof Template2.html:

value1 value2

Real Output:

value2

Why is the value of 'var1' not output when I call {{block.super}}?

I have the 'django.core.context_processors.request', in my settings.py defined. What am I missing?

Dr.Elch
  • 2,105
  • 2
  • 17
  • 23

2 Answers2

2

template2.html extends template1.html, but that does not mean view2 extends view1. You need to add var1 to the context information in view2.

# view2
return render(request, 'template2.html', {
    'var1': 'value1',
    'var2': 'value2'
})
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • This makes sense to. But my problem is related to breadcrumbs, so the page doesn't necessarily know its parent. Do I have to use Session variables here? Seems a bit excessive to me just for this task... – Dr.Elch Jan 21 '14 at 19:20
1

If you don't provide var1in the context in view2 then how do you think the template will get it ? Change your settings.TEMPLATE_STRING_IF_INVALID to something else than the empty string and you'll know why var1 doesn't show up...

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118