When you use Django for Web development, you often construct an underlying framework template called base.html, and then overload the common parts of the site and the definition blocks in its child templates.
First create a base.html, the source code is:
<! DOCTYPE html>
This template, called base.html, defines a simple HTML framework document, which we will use on the page in our site. The role of a child template is to overload, addor preserve the contents of those blocks.
Now create a new current_datetime.html template to use it:
{% extends "base.html"%} {% block title%} The current time{% endblock%}{% block content%}<p>it are now {{current_date}}.</p>{% endblock%}
Then create a new hours_ahead.html template, the source code is:{% extends "base.html"%} {% block title%} Future time{% endblock%}{% block content%}<p>in {{Hour_offset}} hour (s), it'll be {{next_time}}.</p>{% end Block%}
The above part of the non-HTML tag will explain it again, now in views.py new two functions, index4, and index5, respectively, corresponding to the two templates. The source code is:def index4 (req,offset): offset=int (offset) Next_time=datetime.datetime.now () +datetime.timedelta (hours= Offset) return render_to_response ("hours_ahead.html", {' Hour_offset ': Offset, ' next_time ': next_time}) def index5 (req): now=datetime.datetime.now () return render_to_response (' current_datetime.html ', {' current_date ': Now})
The configuration in the URL is:URL (r ' ^hours_ahead/(\d{1,2}$) ', ' blog.views.index4 '), url (r ' ^current_datetime/$ ', ' blog.views.index5 '),
Now start the server and view the effect in the browser, current_datetime.html:The effect in H ours_ahead.html is:
So two HTML effects are displayed, and also explain the role played in Base.html, two of the HTML used the {% extends%} tag,
This is the inheritance base.html content, when using {% block XXXXX%} {% endblock%}, the middle of the content is inserted in the use of the base.html two tags
In the middle, the redundancy of the code is greatly avoided. Each template contains only its own unique code, no unnecessary parts, and if you want to make site-level design modifications, you only need to
Modify the base.html, and all other templates will immediately reflect the changes.
The above is the Django inheritance using the base.html template.
Django Template Inheritance