from flask import Flask, render_template
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 = Flask(__name__)
app.debug = True
@app.template_global()
def ab(a, b):
return a + b
@app.route("/stu")
def stu():
return render_template("stuinfo.html", stu_info=STUDENT, stu_list=STUDENT_LIST, stu_dict=STUDENT_DICT)
if __name__ == '__main__':
app.run("0.0.0.0", 9527)
用到的html页面
学生信息
{{ stu_info }}
name | age | gender |
{{ stu_info.name }} | {{ stu_info.get("age") }} | {{ stu_info["gender"] }} |
{{ stu_list }}
name | age | gender |
{{ stu.name }} | {{ stu.get("age") }} | {% if stu["gender"] != "男" and stu["gender"] != "女" %} 李杰 {% else %} {{ stu["gender"] }} {% endif %} |
{{ stu_dict }}
{% for foo in stu_dict %}
{{ stu_dict[foo].get("name") }}
{{ stu_dict[foo]["age"] }}
{{ stu_dict[foo].gender }}
{% endfor %}
{% for foo,item in stu_dict.items() %}
{{ foo }}
{{ item.name }}
{% endfor %}
{{ ab(666,2) }}
{% macro my_input(ty,na) %}
请输入用户名 ->
{% endmacro %}
这就是我自己创造的input标签:{{ my_input("text","username") }} {{ my_input("password","pass") }} {{ my_input("file","myfile") }}