Rendering data
data preparation
Several strings define a rear end, a front end for delivery to
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': '女'}, }
Process control Jinja2 template
Logical Syntax
Jinja2 template language for
{% for foo in g %} {% endfor %}
Jinja2 template language if
{% if g %} {% elif g %} {% else %} {% endif %}
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
<!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>
Back-end 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)
2. STUDENT_LIST list of incoming operating front-end template Jinja2
Front page
<!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>
We use three values above manner, where each dictionary is foo
Back-end 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)
3.STUDENT_DICT Dictionary of incoming front-end template Jinja2
Front page
<!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>
Back-end 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)
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,
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)
Front page generated label:
<input type="text" name="uname">