美文网首页
flask中加载static和tenplates, 配置cook

flask中加载static和tenplates, 配置cook

作者: 裴general | 来源:发表于2018-05-16 18:18 被阅读0次

摘要

创建了应用app,在app中的views.py中导入静态文件static和模板文件templates的路径发生了变化,需要配置位置,才能恢复使用

1. 加载Static,templates文件

(1)设置基本路径BASE_DIR
os.path.abs(file):当前文件的绝对位置
os.path.dirname()文件的上一级路径

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)应用到文件中

 app = Flask(__name__, template_folder=templates_dir, static_folder=static_dir)

2.设置cookie

(1)添加cookie
小本本:响应, 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='' 设置过期时间, 加载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

相关文章

网友评论

      本文标题:flask中加载static和tenplates, 配置cook

      本文链接:https://www.haomeiwen.com/subject/nglmdftx.html