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