【Flask】jinja2

Rendering data

data preparation

Several strings define a rear end, a front end for delivery to

Copy the code
STUDENT = {'name': 'Old', 'age': 38, 'gender': '中'},

STUDENT_LIST = [
    {'name': 'Old', 'age': 38, 'gender': '中'},
    {'name': 'Boy', 'age': 73, 'gender': '男'},
    {'name': 'EDU', 'age': 84, 'gender': '女'}
]

STUDENT_DICT = {
    1: {'name': 'Old', 'age': 38, 'gender': '中'},
    2: {'name': 'Boy', 'age': 73, 'gender': '男'},
    3: {'name': 'EDU', 'age': 84, 'gender': '女'},
}
Copy the code

Process control Jinja2 template

Logical Syntax

Jinja2 template language for

{% for foo in g %}

{% endfor %}

Jinja2 template language if

Copy the code
{% if g %}

{% elif g %}
    
{% else %}
    
{% endif %}
Copy the code

variable

{{}} 

Next, we passed each of these situations, and displayed as a table in the front

1. STUDENT dictionary is transmitted to the front end

Front page

Copy the code
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1px">
    <thead>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
        </tr>
    </thead>
    <tbody>
        {% for foo in stu %}
            <tr>
                <td>{{ foo.name }}</td>
                <td>{{ foo.age }}</td>
                <td>{{ foo.gender }}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>
</body>
</html>
Copy the code

Back-end code

Copy the code
from flask import Flask,render_template,request
app = Flask(__name__)
STUDENT = {'name': 'Old', 'age': 38, 'gender': '中'},

@app.route('/login',methods=["POST","GET"])
def login():
    if request.method == "GET":
        return render_template("login.html",stu=STUDENT)

if __name__ == '__main__':
    app.run("0.0.0.0", 9876)
Copy the code

2. STUDENT_LIST list of incoming operating front-end template Jinja2

Front page

Copy the code
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1px">
    <thead>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
        </tr>
    </thead>
    <tbody>
        {% for foo in stu %}
            <tr>
                <td>{{ foo.get("name") }}</td>
                <td>{{ foo.age }}</td>
                <td>{{ foo["gender"] }}</td>
            </tr>
        {% endfor %}

    </tbody>
</table>

</body>
</html>
Copy the code

We use three values ​​above manner, where each dictionary is foo

Back-end code

Copy the code
from flask import Flask,render_template,request
app = Flask(__name__)

STUDENT_LIST = [
    {'name': 'Old', 'age': 38, 'gender': '中'},
    {'name': 'Boy', 'age': 73, 'gender': '男'},
    {'name': 'EDU', 'age': 84, 'gender': '女'}
]

@app.route('/login',methods=["POST","GET"])
def login():
    if request.method == "GET":
        return render_template("login.html",stu=STUDENT_LIST)

if __name__ == '__main__':
    app.run("0.0.0.0", 9876)
Copy the code

3.STUDENT_DICT Dictionary of incoming front-end template Jinja2

 Front page

Copy the code
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1px">
    <thead>
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
        </tr>
    </thead>
    <tbody>
        {% for foo in stu %}
            <tr>
                <td>{{ foo }}</td>
                <td>{{ stu.get(foo).name }}</td>
                <td>{{ stu.get(foo).age }}</td>
                <td>{{ stu.get(foo).gender }}</td>

            </tr>
        {% endfor %}

    </tbody>
</table>

</body>
</html>
Copy the code

Back-end code

Copy the code
from flask import Flask,render_template,request
app = Flask(__name__)

STUDENT_DICT = {
    1: {'name': 'Old', 'age': 38, 'gender': '中'},
    2: {'name': 'Boy', 'age': 73, 'gender': '男'},
    3: {'name': 'EDU', 'age': 84, 'gender': '女'},
}
@app.route('/login',methods=["POST","GET"])
def login():
    if request.method == "GET":
        return render_template("login.html",stu=STUDENT_DICT)

if __name__ == '__main__':
    app.run("0.0.0.0", 9876)
Copy the code

When traversing the dictionary, foo actually come up with the equivalent of a dictionary Key

 

Markup

The method and the safe are the same as in django prevent xss attacks,

Copy the code
from flask import Flask,render_template,request
from markupsafe import Markup

app = Flask(__name__)

@app.route('/login',methods=["POST","GET"])
def login():
    if request.method == "GET":
        my_in = Markup("<input type='text' name='uname'>")
        return render_template("login.html",ss=my_in)

if __name__ == '__main__':
    app.run("0.0.0.0", 9876)
Copy the code

Front page generated label:

<input type="text" name="uname">

Guess you like

Origin www.cnblogs.com/youxiu123/p/11605774.html