Django template extends

Source: Internet
Author: User
Tags django tutorial

Let's talk about it today.Django template extends

When I was writing PHP, I usually separated all public templates, such as the page header and the bottom of the page,
Then use require 'footer. php' and so on.
In this way, if you want to modify the header and the backend connection. You only need to modify two files.
At first, I thought it was very useful. But after I got started with Django. Django template inheritance function found
More powerful than PHP ..

For example, we write an index. php
<HTML>
<Head>
<Title>
Test
</Title>
<Div id = "daohang">
Homepage Python tutorial Django tutorial about Old Wang Python
</Div>
</Head>
<Body>
This is Index
</Body>
</Html>

For PHP:
I will create a new header. php
Then
<HTML>
....
</Head>
All the contents are put in header. php.
Then, call header. php In index. php.
The current content of index. php is:
<? PHP require_once ('header. php');?>
<Body>
This is Index
</Body>
</Html>

For Django
I will create a new base.html main template file, which mainly describes the overall architecture:
Base.html:
<HTML>
<Head>
<Title>
Test
</Title>
<Div id = "daohang">
Homepage Python tutorial Django tutorial about Old Wang Python
</Div>
</Head>
<Body>
{% Block content %}
This is Index
{% Endblock %}
</Body>
</Html>

If a page is similar to a list.html page, its header and bottom are the same as the homepage. For PHP.
Write list.html as follows:
<? PHP require_once ('header. php');?>
<Body>
This is list
</Body>
</Html>
However, it is convenient to use the extends of Django template.
List.html:
{% Entends base.html %} If extends is defined here, the defined sub-template can inherit the parent template.
<Body>
{% Block content %}
This is list # If block is defined here, the sub-template we define can overwrite the part of content defined by the parent template.
{% Endblock %}
</Body>
</Html>
Through comparison between Django template and PHP, we feel that the flexibility and efficiency of Django development projects are faster than those of PHP ..
However, PHP is much faster than Django template. Haha. Each method has advantages and disadvantages, and when to use it ..

In the first few articles, I wrote an article about Django philosophy. If you are interested, take a look.

author: Old Wang @ Python tutorial
old Wang Python provides Python tutorials and Python downloads related to pythn. I hope you will like it

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.