Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is {% block content %} and {% endblock content %} for in Django?

Tags:

python

django

so I just started reading a book on Django (for beginners) and I came across the following code snipet:

<header>  <a href="{% url 'home' %}">Home</a> | <a href="{% url 'about' %}">About</a>  </header>  {% block content %} {% endblock content %} 

Could anyone possibly explain to me what is the use of {% block content %} and {% endblock content %}? Thank you very much in advance!

like image 779
Nazim Kerimbekov Avatar asked Nov 19 '18 22:11

Nazim Kerimbekov


People also ask

What is {% block content %} in Django?

Introducing {% block %} The block tag is used to define a block that can be overridden by child templates. In other words, when you define a block in the base template, you're saying that this area will be populated with content from a different, child template file.

What does {% include %} do in Django?

From the documentation: {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

What does {% include %} do?

Q13:-What does {% include %} does? It will include another template. It will include content from another template having the same templates defined.

What does block mean in Django?

block is used for overriding specific parts of a template. In your case, you have a block named content and this is supposed to be overridden by children that inherit from this template. From the examples at The Django Docs.


Video Answer


2 Answers

block is used for overriding specific parts of a template.

In your case, you have a block named content and this is supposed to be overridden by children that inherit from this template.

From the examples at The Django Docs

Template to be extended, named base.html

<head>     <link rel="stylesheet" href="style.css">     <title>{% block title %}My amazing site{% endblock %}</title> </head> 

Overriding Child template

{% extends "base.html" %}  {% block title %}My amazing blog{% endblock %} 

"My amazing site" will be overriden by the child and then display "My amazing blog"

like image 52
Cup of Java Avatar answered Sep 25 '22 09:09

Cup of Java


That's where the power of the templates comes from in a sense.

You can create a hierarchy of templates so start with base.html which might be like you've got above;

<body>     {% block content %}     {% endblock content %} </body> 

Then you can create any other template, home.html for example, and do something like;

{% extends "base.html" %}  {% block content %}     <h1>Welcome</h1>     <p>This is the home page</p> {% endblock content %} 

Then you'd reference home.html in django and it'd include the markup from base.py with the content defined in home.html.

That's the basics, but if you put some templates together using blocks you'll pick it up.

like image 45
markwalker_ Avatar answered Sep 22 '22 09:09

markwalker_