Inheritance and block of flask


In the process of web development, there are many similar page parts, such as menus. How to implement them?

You can take the template, and then inherit the template. In the following example, base.html is the template file.

The reference page uses {% base. HTML ‘base. HTML’%} to inherit the template.

But not all the content of each page is the same, how to deal with it differently?

The sub template uses block. In Python, you define your own attributes in the sub class, but the Web Implementation of falsk is not. You need to define the interface in the parent template base.html

The contents are as follows:

The code of template base.html is as follows:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
            background: #3a3a3a;
            height: 65px;
            overflow: hidden;
        ul li{
            float: left;
            list-style: none;
            padding: 0 10px;
            line-height: 65px;
        ul li a{
            color: #ffffff;
    <div class="nav">
            <li>< a href = "ා" > Home Page</a></li>
            <li>< a href = "ා" > score query</a></li>
    {% block main %}{% endblock %}

The extend block demo.html code is as follows:

{% extends 'base.html' %}
{% block main %}
    <h1>This is the front page</h1>
{% endblock %}
The code of extend? Block? Login? Demo.html is as follows:

{% extends 'base.html' %}
{% block main %}
    <h1>This is the login page</h1>
{% endblock %}

The code of extend? Block? is as follows:

from flask import Flask, render_template
app = Flask(__name__)
def hello_world():
    return render_template('extend_block_demo.html')
def login():
    return render_template('exctend_block_login_demo.html')
if __name__ == '__main__':


Recommended Today

PHP creates blockchain

Preceding words Provide a thought to help you understand the basic operation of blockchain. The text is not a complete blockchain. I hope you can draw some examples Source code:… remember Blockchain is an immutable and orderly record chain called block. They can contain transactions, files, or any data you like. But what’s important […]