from flask import Flask, request,render_template,jsonify,make_response,redirect
from flask_script import Manager
1.# 獲取flask對(duì)象
from utils.functions import login_required
app = Flask(__name__)
2.# 使用router綁定路由和執(zhí)行的視圖函數(shù)的關(guān)聯(lián)關(guān)系
@app.route('/hello/')
def hello():
return 'hello world'
3.定義參數(shù)規(guī)則# 路由規(guī)則,定義: <轉(zhuǎn)換器:參數(shù)名>
# django中路由定義: int參數(shù): path('/uid/<int:id>/', xxxx)
# 字符串參數(shù):path('/uid/<name>/', xxxx)
# 字符串參數(shù):path('/uid/<str:name>/', xxxx)
# uuid參數(shù):path('/uid/<uuid:uid>/', xxxx)
# path參數(shù):path('/uid/<path:uid>/', xxxx)
# 正則表達(dá)式 Django2.0以上 re_path('/uid/(\d+)/')
# 正則表達(dá)式 Django2.0以下 url('/uid/(\d+)/')
# url('/uid/(?P<id>\d+)/')
# flask中路由定義: int參數(shù): route('/uid/<int:id>/')
# 字符串: route(‘/uid/<name>/’)
# 字符串: route(‘/uid/<string:name>/’)
# uuid: route(‘/uid/<uuid:uid>/’)
# float: route(‘/uid/<float:uid>/’)
# path路徑: route(‘/uid/<path:uid>/’)
@app.route('/uid/<int:id>/')
def get_id(id):
return 'id: {}'.format(id)
@app.route('/uname/<name>/')
def get_name(name):
print(type(name))
return 'name:{uname}'.format(uname=name)
@app.route('/uname2/<string:name>/')
def get_name2(name):
return 'name2:{}'.format(name)
@app.route('/uuid/<uuid:uid>/')
def get_uid(uid):
return 'uid:%s'% uid
@app.route('/price/<float:p>/')
def get_p(p):
return 'price:{}'.format(p)
@app.route('/path/<path:pth>/')
def get_pth(pth):
return 'path:{}'.format(pth)
4. request請(qǐng)求方法
# @app.route('/res/',methods=['GET','POST','PUT'])
# def res():
# if request.method == 'GET':
# return 'GET請(qǐng)求響應(yīng)'
# if request.method == 'POST':
# return 'POST請(qǐng)求'
@app.route('/res/',methods=['GET','POST','PUT'])
def res():
if request.method == 'GET':
# django中GET取值: request.GET.get() request.GET.getlist()
# flask中GET取值: request.args.get(key) request.args.getlist(key)
names = request.args.getlist('name')
age = request.args.get('age')
return 'GET'
if request.method == 'POST':
# django中POST取值: request.POST.get() / getlist()
# flask中POST取值: request.form.get() / getlist()
names = request.form.getlist('name')
age = request.form.get('age')
return 'POST'
# 上傳文件
request.files
# 獲取cookie
request.cookies
# 方式
request.method
# 路由
request.path
if request.method == 'PUT':
# put、patch、delete取值方式和post一樣
# request.form.get() 、 getlist()
return 'PUT請(qǐng)求響應(yīng)'
@app.route('/req/', methods=['GET', 'POST'])
def req():
if request.method == 'GET':
# 響應(yīng)字符串
# return 'GET請(qǐng)求響應(yīng)'
# 響應(yīng)頁面
# return render_template('index.html')
# 響應(yīng)json數(shù)據(jù)
# return jsonify({'code': 200, 'msg': '請(qǐng)求成功'})
# 響應(yīng)中綁定cookie參數(shù)
# 在Django中: HttpResponse()/redirect()/HttpResponseRedirect()
# 在Flask中: make_response()
# response = make_response(render_template('index.html'), 200)
# response.set_cookie('token', '124567890', max_age=3000)
# response.delete_cookie('token')
# return response
# 重定向(跳轉(zhuǎn))
return redirect('/hello/')
5.渲染頁面
@app.route('/login/',methods=['GET','POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if username == 'aa' and password == '123456':
# 登陸成功,跳轉(zhuǎn)到首頁
# redirect和render_template區(qū)別
res = redirect('/index/')
res.set_cookie('token','123456',max_age=3000)
return res
return render_template('login.html')
@app.route('/index/')
@login_required
def index():
return render_template('index.html')
if __name__ == '__main__':
# 啟動(dòng)
# 解析命令行中傳遞的參數(shù)
# print(sys.argv)
# host = sys.argv[1]
# port = sys.argv[2]
# app.run(host=host, port=port, debug=True)
# 使用Manager管理flask對(duì)象
manage = Manager(app)
# 啟動(dòng)命令行: python hello.py runserver -h -p -d
manage.run()
6.裝飾器
# 1.外層函數(shù)嵌套內(nèi)層函數(shù)
# 2.外層函數(shù)返回內(nèi)層函數(shù)
# 3.內(nèi)層函數(shù)使用外層函數(shù)傳遞的參數(shù)
# 裝飾器 兩層函數(shù),外層調(diào)用內(nèi)層函數(shù)
# 內(nèi)測函數(shù)實(shí)現(xiàn)功能后調(diào)用原函數(shù) 返回原函數(shù)結(jié)果
from functools import wraps
from flask import request
from werkzeug.utils import redirect
def login_required(func):
# func是被裝飾的函數(shù)
@wraps(func)
def check():
token = request.cookies.get('token')
if not token:
return redirect('/login/')
if token != '123456':
return redirect('/login/')
return func()
return check
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。