Flask中jinja2的应用
# -*- coding: utf-8 -*-
# @Time : 2019/9/24 17:29
# @Author : AnWen
from flask import Flask, render_template
app = Flask(__name__)
app.debug = True
# 数据
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': '女'},
}
def func(a, b):
return a + b
@app.route('/stu')
def stu():
return render_template('stu.html', func=func ,stu_info=STUDENT, stu_list=STUDENT_LIST, stu_dic=STUDENT_DICT)
if __name__ == '__main__':
app.run()
html文件
Titlehaha
{{ stu_info }}
nameagegender
{{ stu_info.name }}{{ stu_info.age }}{{ stu_info["gender"] }}{{ stu_list }}
nameagegender
{% for stu in stu_list %}
{{ stu.name }}{{ stu.get('age') }}{% if stu["gender"] !="男" and stu['gender'] !="女" %}
alal
{% else %}
{{ stu["gender"] }}
{% endif %}
{% endfor %}
{{ stu_dic }}
{% for dic in stu_dic %}
{{ stu_dic[dic].get("name") }}
{{ stu_dic[dic]["age"] }}
{{ stu_dic[dic].gender }}
{% endfor %}
idnameagegender
{% for key,item in stu_dic.items() %}
{{ key }}{{ item.name }}{{ item.age }}{{ item.gender }}{% endfor %}
{{ func(2,3) }}
{% macro my_input(type,name) %}
{% endmacro %}
自己创建的input标签 :{{ my_input("text","username") }} {{ my_input("password","pass") }} {{ my_input("file","myfile") }}