摘要
創(chuàng)建了應(yīng)用app萧诫,在app中的views.py中導(dǎo)入靜態(tài)文件static和模板文件templates的路徑發(fā)生了變化,需要配置位置枝嘶,才能恢復(fù)使用
1. 加載Static,templates文件
(1)設(shè)置基本路徑BASE_DIR
os.path.abs(file):當(dāng)前文件的絕對(duì)位置
os.path.dirname()文件的上一級(jí)路徑
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
(2)加載static和templates
templates_dir = os.path.join(BASE_DIR, 'templates')
static_dir = os.path.join(BASE_DIR, 'static')
(3)應(yīng)用到文件中
app = Flask(__name__, template_folder=templates_dir, static_folder=static_dir)
2.設(shè)置cookie
(1)添加cookie
小本本:響應(yīng)帘饶, make_response()
@blue.route('/getresponse/')
def get_response():
response = make_response('<h2>大帥比</h2>', 200)
ticket = ''
s = 'asqwertyuopksafjklacz,.c,mzmvmlk;klsmc'
for i in range(20):
ticket += random.choice(s)
# , max_age='', expire='' 設(shè)置過(guò)期時(shí)間, 加載set_cookie的后面
response.set_cookie('ticket', ticket, max_age=20000)
return response
(2)刪除cookie
@blue.route('/delresponse/')
def del_cookie():
response = make_response('<h2>大帥比</h2>', 200)
response.delete_cookie('ticket')
return response