关于Flask高级_钩子函数的介绍和使用

Flask高级_钩子函数

一.介绍

钩子函数概念 在Flask中钩子函数是使用特定的装饰器装饰的函数。
为什么叫做钩子函数呢,是因为钩子函数可以在正常执行的代码 中,插入一段自己想要执行的代码。 那么这种函数就叫做钩子函数。
常见的钩子函数:
  • before_first_request:处理项目的第一次请求之前执行。
  • before_request:在每次请求之前执行。通常可以用这个装饰 器来给视图函数增加一些变量。请求已经到达了Flask,但是还 没有进入到具体的视图函数之前调用。一般这个就是在视图函数 之前,我们可以把一些后面需要用到的数据先处理好,方便视图 函数使用。
  • teardown_appcontext:不管是否有异常,注册的函数都会在 每次请求之后执行。
  • template_filter:在使用Jinja2模板的时候自定义过滤器。
  • context_processor:上下文处理器。使用这个钩子函数,必须 返回一个字典。这个字典中的值在所有模版中都可以使用。这个 钩子函数的函数是,如果一些在很多模版中都要用到的变量,那 么就可以使用这个钩子函数来返回,而不用在每个视图函数中 的 render_template 中去写,这样可以让代码更加简洁和好维护。
  • errorhandler:errorhandler接收状态码,可以自定义返回这 种状态码的响应的处理方法。在发生一些异常的时候,比如404 错误,比如500错误,那么如果想要优雅的处理这些错误,就可以 使用 errorhandler 来出来。

二.实例

  • before_first_request与before_request
    #coding=utf-8
    
    from flask import Flask,request
    
    app = Flask(__name__)
    
    @app.route('/')
    def show():
        return "Hello~"
    
    @app.before_first_request
    def firstp():
        print('第一次请求之前打印这段内容的内容,之后的请求都不会再打印这个内容了')
        return 'firstp'
    
    @app.before_request
    def everyp():
        print('每次请求之前都会打印这段内容')
    
    if __name__ == '__main__':
        app.run(debug=True)
    

在这里插入图片描述

  • context_processor
    templates文件夹内容:index.html与home,html
    index.html:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>主页</title>
    </head>
    <body>
        主页内容
        <p>请登录:{{ uname }}</p>
    </body>
    </html>
    
    home.html:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>个人中心</title>
    </head>
    <body>
        个人中心内容
        <p>用户名:{{ uname }}</p>
    </body>
    </html>
    
    py文件:
    #coding=utf-8
    
    from flask import Flask,render_template
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        print('2222222')
        return render_template('index.html')
    
    @app.route('/home/')
    def home():
        print('33333333')
        return render_template('home.html')
    
    @app.context_processor
    def text():
        print('11111111')
        return {'uname':'hh'}
    
    if __name__ == '__main__':
        app.run(debug=True)
    
  • errorhandler
    templates文件夹内容:500.html与404.html
    500.html:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>500错误页面</title>
    </head>
    <body>
        <h2>500错误</h2>
        <p>服务器炸裂了!!!</p>
    </body>
    </html>
    
    404.html:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>404错误页面</title>
    </head>
    <body>
        <h2>404错误</h2>
        <p>您访问的页面被外星人抓走了!!!</p>
    </body>
    </html>
    
    py文件:
    #coding=utf-8
    
    from flask import Flask,render_template,abort
    
    app = Flask(__name__)
    
    @app.route('/')
    def show():
        print(uname)
        return "Hello~"
    
    @app.errorhandler(500)
    def server_handle(error):
        return render_template('500.html'),500
    
    @app.errorhandler(404)
    def server_handle(error):
        return render_template('404.html'),404
    
    if __name__ == "__main__":
        # app.run(debug=True)
        app.run()
        
    

在这里插入图片描述
在这里插入图片描述

注:
如果觉得笔记有些问题,麻烦在百忙之中在评论中指正,或提出建议!另外,如果觉得这份笔记对你有所帮助,麻烦动动发财的小手手点一波赞!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值