01 …“微”(micro) 并不表示你需要把整个 Web 应用塞进单个 Python 文件
…(虽然确实可以 ),也不意味着 Flask 在功能上有所欠缺。
…微框架中的“微”意味着 Flask 旨在保持核心简单而易于扩展。

1 最简单的调试程序

from flask import Flask #Flask 相当于一个类

app = Flask(__name__) #创建app对象

@app.route("/") #装饰器
def hello_world(): #定义hello_world方法
    return "hello world!!"

if __name__ == "__main__":#仅当前类应用
    app.run()#运行程序

Flask框架初识(—)

2 调试程序(app.debug = True / app.run(debug = True))

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "hello world!!"

@app.route("/main")
def main():
    return "你好"

if __name__ == "__main__":
    #app.run(debug = True) 
    #自动调试,免去改代码时重新启动
    app.debug = True
    app.run()

Flask框架初识(—)

3 外部访问 (app.run(host = (‘0.0.0.0’)))

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "hello world"

if __name__ == "__main__":
    app.run(host = '0.0.0.0')#将自己作为一个服务器 允许外部访问

Flask框架初识(—)

4 变量规则 ( int:password

4 变量规则 (<password>   <int:password>)
from flask import Flask

app = Flask(__name__)

@app.route("/user/<password>")
def hello(password):
    print(type(password))
    return "hahaha"

if __name__ == "__main__":
    app.run()

Flask框架初识(—)

int float path(/) 转换类型
int 接受整数
float 同 int ,但是接受浮点数
path 和默认的相似,但也接受斜线

from flask import Flask

app = Flask(__name__)

@app.route("/user/<int:password>")
def hello(password):
    print(type(password))
    return "hiahia"

if __name__ == "__main__":
    app.run()

Flask框架初识(—)

唯一 URL / 重定向行为
5 唯一 URL(/index) / 重定向行为(/index/)
(404–Not Found / 200–成功 / 400–参数错误 /500–逻辑代码错误)

from flask import Flask

app = Flask(__name__)

@app.route("/index")#重定向行为
def hello():
    return "hello world"

@app.route("/index/") #唯一URL
def hello1():
    return "hello world1"

if __name__ == "__main__":
    app.run()

Flask框架初识(—)

6 构造URL url_for test_request_context

from flask import Flask,url_for

app = Flask(__name__)

@app.route('/<name>')
def hello(name):
    return name

@app.route('/main')
def main():
    return "你好"

if __name__ == "__main__":
    with app.test_request_context():
        print(url_for('hello',name = "asd"))
    app.run()

Flask框架初识(—)

7 Http方法
get—暴露传输参数 参数大小有限制
post—不暴露传输参数 参数大小几乎无限制
数据角度:一样
安全角度:加密
http://127.0.0.1:5000/main?username=aaa&password=aaa
http://127.0.0.1:5000/main

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action = "http://127.0.0.1:5000/main" method = post>
        <input type = "text" value = "" name = "username">
        <input type = "password" value = "" name = "password">
        <input type = "submit" value = "提交">
    </form>
</body>
</html>
from flask import Flask,url_for

app = Flask(__name__)

@app.route('/<name>')
def hello(name):
    return name

@app.route('/main',methods = ["post"])
def main():
    return "你好"

if __name__ == "__main__":
    app.run()

Flask框架初识(—)

templates static (约定大于配置)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="background-image: url('/static/background1.jpg')">
    <form action = "http://127.0.0.1:5000/login" method = "post">
        用户名:<input type = "text" name = "username">
        密码:<input type = "password" name = "password">
        <input type = "submit" value = "提交">
    </form>
</body>
</html>
from flask import Flask,url_for,render_template

app = Flask(__name__)

#跳转到index.html 登录页面
@app.route('/index/')
def hello():
    return render_template("index.html")

#接收参数 验证登录成功/失败
@app.route('/login',methods = ["GET","POST"])
def login():
    print("假装验证")
    return "success"
if __name__ == "__main__":
    with app.test_request_context():
        print(url_for('hello', name="asd"))
    app.debug = True
    app.run()

Flask框架初识(—)

#request(静态文件)

#request(静态文件)
#10 文件上传
from flask import Flask,url_for,render_template,request

app = Flask(__name__)

#跳转登录页面
@app.route('/index/')
def hello():
    return render_template("index.html")

#接收参数做验证
@app.route('/login/',methods = ["GET","POST"])
def login():
    username = request.args.get("username")
    password = request.args.get("password")
    if username =="admin" and password == "123":
        return render_template("success.html",username = "admin")
    return render_template("index.html",error = "*用户名或密码错误!!")
if __name__ == "__main__":
    with app.test_request_context():
        print(url_for('hello', name="asd"))
    app.debug = True
    app.run()
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>登录成功,欢迎{{username}}</h1>
</body>
</html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
        <form action="http://127.0.0.1:5000/login/" method="get">
        用户名:<input type="text" name="username">
        密码:<input type="password" name="password">
        <input type="submit" value="提交">
    </form>
    <p style="color: red">{{ error }}</p>
</body>
</body>
</html>

Flask框架初识(—)

相关文章: