Django1.9 Development Blog (6)-Template inheritance

Source: Internet
Author: User

Template inheritance is that multiple pages of a site can share the same page layout or content that is part of a page. In this way you will need to copy and paste the same code on each page. If you want to change the public part of the page, you do not need to modify each page, you just need to modify a template on the line, so that maximize reuse, reduce redundancy, but also reduce the chance of error, and you hit the code is also less.

Create a base template

A base template is the most basic site framework template that you will inherit from all the pages in your entire station. We create a base.html template in blog/templates/blog/:

blog└───templates    └───blog            base.html            post_list.html

Open base.html, and then copy all of the contents of post_list.html, and now it reads as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
{% load staticfiles%}
<Html>
<Head>
<LinkRel="Stylesheet"href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" >
<LinkRel="Stylesheet"href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css" >
<ScriptSrc="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js" ></Script>
<Linkhref="Http://fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext"Rel="Stylesheet"Type="Text/css" >
<LinkRel="Stylesheet"href="{% static ' css/blog.css '%}" >
<Title>django Girls Blog</Title>
</Head>
<Body>
<Divclass="Page-header" >
<H1><Ahref="/" >django Girls Blog</A></H1>
</Div>
<Divclass="Content" >
<Divclass="Row" >
<Divclass="Col-md-8" >
{% for post in posts%}
<Divclass="POST" >
<H1><Ahref="" >{{Post.title}}</A></H1>
<p>published: {{post.published_date}}</p>
<p>{{Post.text|linebreaks}}</p>
</div>
{% ENDFOR%}
</div>
</div>
</div>
</body>
</html>

In the base.html, the ... Replace the block with the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
<Body>
<Divclass="Page-header" >
<H1><Ahref="/" >django Girls Blog</A></H1>
</DIV>
<div class= <div class= <div class= Span class= "line" and {% block content%}
{% Endblock%}
& Lt;/DIV>
</DIV>
Span class= "line" > </div>,
</BODY>

We're actually going to {% for post in posts %}{% endfor %} replace it {% block content %}{% endblock %} . In Base.html we created a block with a name of content, and other pages can be inherited base.html, replace the content block to generate a new page, and the rest of the page remains intact.

After saving, modify the post_list.html page to keep only the content:

1
2
3
4
5
6
7
{% for post in posts%}
<div class= "post";
<h1><a href=</a>< span class= "tag" ></H1>
< P>published: {{post.published_date}}</P>
span class= "tag" ><p>{{post.text|linebreaks}}</ P>,
</DIV>
{% endfor%}
/span>

Then add this sentence to the beginning of the post_list.html page:

{% extends ' blog/base.html '%}

This means that the template inherits from the blog/base.html template

One more step is to put the content you just had in {% block content%} and {% Endblock content%}, when the entire page was:

1
2
3
4
5
6
7
8
9
10
{% extends ' blog/base.html '%}
{% block content%}
{% for post in posts%}
<Divclass= "post";
< h1><a href= </a>< /H1>
<p>published: {{ Post.published_date}}</P>
<p>{{post.text|linebreaks}}</P>
</DIV>
{% endfor%}
{% endblock content%}

Refresh the page after saving to see if it will work correctly:

Django1.9 Development Blog (6)-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.