• 关于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)
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21

    在这里插入图片描述

    • 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>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      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>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      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)
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
    • 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>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      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>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      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()
          
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23

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

    注:
    如果觉得笔记有些问题,麻烦在百忙之中在评论中指正,或提出建议!另外,如果觉得这份笔记对你有所帮助,麻烦动动发财的小手手点一波赞!
  • 相关阅读:
    Mathematica求解方程——Solve、Reduce、NSolve等函数
    win11安装ubuntu(by wsl2)
    Vue3 与 django 进行 前后端数据交互之(Vue 上传)
    Ubuntu Budgie 22.04 设置中文语言并安装拼音输入法
    java-php-python-ssm基于网络的课程答疑系统计算机毕业设计
    Python+OpenCV4马赛克图片合成
    C. Card Game(dp&组合数)
    外呼系统用回拨模式打电话有什么优势
    Golang入门笔记(最后一章 17 封版)—— 映射map
    java整合快手小程序(登陆,支付,结算,退款,手机号授权登陆)
  • 原文地址:https://blog.csdn.net/qq_55961861/article/details/126581736