# 创建项目
jinja2 语法基础
# pycharm 里面 创建 new project -->pure python 之后选择路径 选择解释器 以及虚拟环境问题from flask import Flask,request,redirect,render_template,jsonify,send_fileapp = 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)
Old Boy EDU _____________________________________Welcome to Old Boy EDU : student{ { student }}
{ { student.name }} | { { student["age"] }} | { { student.get("gender") }} |
_____________________________________
Welcome to Old Boy EDU : student_list { { student_list }}
{ { foo }} | { { foo.name }} | { { foo.get("age") }} | { { foo["gender"] }} |
_____________________________________
Welcome to Old Boy EDU : student_dict { { student_dict }}
{ { foo }} | { { student_dict.get(foo).name }} | { { student_dict[foo].get("age") }} | { { student_dict[foo]["gender"] }} |
jinja2 高阶
safe : 此时你与HTML只差一个 safe
后端from flask import Flaskfrom flask import render_templateapp = Flask(__name__)@app.route("/")def index(): tag = "" return render_template("index.html",tag=tag)app.run("0.0.0.0",5000)