码迷,mamicode.com
首页 > 其他好文 > 详细

flask 2 进阶

时间:2019-06-12 01:18:03      阅读:118      评论:0      收藏:0      [点我收藏+]

标签: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)
一个主py文件
技术图片
<!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)

 

 

flask 2 进阶

标签:use   get   __name__   debug   nbsp   基础   src   进阶   value   

原文地址:https://www.cnblogs.com/zhangchen-sx/p/11006945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!