Django Template Inheritance

Source: Internet
Author: User

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, add

or 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.