标签:use get __name__ debug nbsp 基础 src 进阶 value
# 创建项目
jinja2 语法基础
# pycharm 里面 创建 new project -->pure python 之后选择路径 选择解释器 以及虚拟环境问题 from flask import Flask,request,redirect,render_template,jsonify,send_file app = Flask(__name__) 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‘: ‘女‘}, } @app.route(‘/‘) #根目录 def index(): return ‘Hello Life‘ @app.route(‘/stu‘) #基本字典 def stu(): return render_template(‘index.html‘,student=STUDENT) @app.route(‘/stu1‘) #列表 def stu1(): return render_template(‘index1.html‘,student=STUDENT_LIST) @app.route(‘/stu2‘) #字典 def stu2(): return render_template(‘index2.html‘,student=STUDENT_DICT) if __name__ == ‘__main__‘: app.run(‘0.0.0.0‘,6900,debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Old Boy EDU</title> </head> <body> <div> _____________________________________</div> Welcome to Old Boy EDU : student <div>{{ student }}</div> <table border="1px"> <tr> <td>{{ student.name }}</td> <td>{{ student["age"] }}</td> <td>{{ student.get("gender") }}</td> </tr> </table> <div> _____________________________________</div> Welcome to Old Boy EDU : student_list <div>{{ student_list }}</div> <table border="1xp"> {% for foo in student_list %} <tr> <td>{{ foo }}</td> <td>{{ foo.name }}</td> <td>{{ foo.get("age") }}</td> <td>{{ foo["gender"] }}</td> </tr> {% endfor %} </table> <div> _____________________________________</div> Welcome to Old Boy EDU : student_dict <div>{{ student_dict }}</div> <table border="1xp"> {% for foo in student_dict %} <tr> <td>{{ foo }}</td> <td>{{ student_dict.get(foo).name }}</td> <td>{{ student_dict[foo].get("age") }}</td> <td>{{ student_dict[foo]["gender"] }}</td> </tr> {% endfor %} </table> </body> </html>
jinja2 高阶
safe : 此时你与HTML只差一个 safe
后端 from flask import Flask from flask import render_template app = Flask(__name__) @app.route("/") def index(): tag = "<input type=‘text‘ name=‘user‘ value=‘DragonFire‘>" return render_template("index.html",tag=tag) app.run("0.0.0.0",5000)
标签:use get __name__ debug nbsp 基础 src 进阶 value
原文地址:https://www.cnblogs.com/zhangchen-sx/p/11006945.html