Inheritance and block of flask

Time:2020-1-19

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">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .nav{
            background: #3a3a3a;
            height: 65px;
        }
        ul{
            overflow: hidden;
        }
        ul li{
            float: left;
            list-style: none;
            padding: 0 10px;
            line-height: 65px;
        }
        ul li a{
            color: #ffffff;
        }
    </style>
</head>
<body>
    <div class="nav">
        <ul>
            <li>< a href = "ා" > Home Page</a></li>
            <li>< a href = "ා" > score query</a></li>
        </ul>
    </div>
    
    {% block main %}{% endblock %}
</body>
</html>

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? Demo.py is as follows:

from flask import Flask, render_template
 
app = Flask(__name__)
 
 
@app.route('/extend_block_demo/')
def hello_world():
    return render_template('extend_block_demo.html')
 
 
@app.route('/login/')
def login():
    return render_template('exctend_block_login_demo.html')
 
 
if __name__ == '__main__':
    app.run(debug=True)

 

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: https://github.com/ar414-com/… 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 […]