66
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”
24
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.
- [Django]-Rendering a template variable as HTML
- [Django]-UUID as default value in Django model
- [Django]-How to make Django's DateTimeField optional?
2
For example, you have code excerpts from 2 files:
base.html:
<body bgcolor="cyan">
{% block content %}
{% endblock %}
</body>
home.html:
{% extends 'base.html' %}
{% block content %}
<h1>Hello World from Abhishek</h1>
{% endblock %}
here in home.html, the attributes of base.html will be extended but by using {% block content %}
and {% endblock %}
you will be able to override the code block of home.html upon the attributes of base.html
This is Jinja template for a dynamic website.
- [Django]-Django QuerySet order
- [Django]-Django template display item value or empty string
- [Django]-Django error message "Add a related_name argument to the definition"
1
That is simply django’s template inheritance. It basically means that a block of content from another html file has been imported into that html file you’re currently on.
- [Django]-Is it better to use path() or url() in urls.py for django 2.0?
- [Django]-How to get Gunicorn to use Python 3 instead of Python 2 (502 Bad Gateway)
- [Django]-How to get Django and ReactJS to work together?