Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

python - what is {% block content %} and {% endblock content %} for in 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...