Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPachal Debbarma
Courses Plus Student 3,597 Pointswhat is the purpose of {{block.super}} in django template?
Can anybody tell me what is block super in the case of django template language?
2 Answers
Avery Uslaner
15,099 PointsSure. So lets create a block in example.html like so:
{% block content %}
<p>Some example text.</p>
{% endblock %}
And then we can extend that block in a second template:
{% extends 'example.html' %}
{% block content %}
<p>The previous example text is replaced by this text.</p>
{% endblock %}
In the example above, only the latter sentence would show up. "Some example text." is being overridden and replaced by "The previous example text is replaced by this text.". You would use {{ block.super }} if you want both things to show up:
{% extends 'example.html' %}
{% block content %}
{{ block.super }}
<p>The previous example text is replaced by this text.</p>
{% endblock %}
So with the call to super, you can render whatever was declared in the first template rather than replacing it whatever you put in the template that extends it.
Avery Uslaner
15,099 PointsFrom the Django documention: "If you need to get the content of the block from the parent template, the {{ block.super }} variable will do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it. Data inserted using {{ block.super }} will not be automatically escaped..."
Pachal Debbarma
Courses Plus Student 3,597 Pointsthanks for you response Avery.But can you give me a small example?Reading documentation is not cleaning my confusion.
Pachal Debbarma
Courses Plus Student 3,597 PointsPachal Debbarma
Courses Plus Student 3,597 PointsNow I got the concept of {% block.super% }.Thank you so much for taking your time and helping me out.Avery
Oziel Perez
61,321 PointsOziel Perez
61,321 PointsIt's basically the same concept for 'super' in any OOP, you call the 'parent' class's constructor to include whatever code is in there, instead of just simply overriding it.