Flask app.py

源码中有这么几个装饰器:

  • before_request(self, f) # 注册一个在请求到达前要执行的函数

  • before_first_request(self, f) # 注册在整个应用实例第一个请求到达前要执行的函数

  • after_request(self, f) # 注册一个在请求处理后的函数

  • teardown_request(self, f) # 注册在请求上下文栈弹出后要执行的函数

  • teardown_appcontext(self, f) # 注册在应用上下文结束时要执行的函数

  • context_processor(self, f) # 向模板上下文中注入变量和方法,这个f必须返回一个字典, 在渲染的模板中使用

  • url_defaults(self, f) # 为应用程序中的所有视图函数 注册URL值预处理器函数。 这些函数将在:before_request函数之前调用。

  • url_value_preprocessor(self, f) # 注册一个 ‘在请求的url匹配后视图函数执行前,把环境中的某些变量换个位置存储的’ 函数

# url_defaults(self, f) 和 url_value_preprocessor(self, f) 的使用
from flask import Blueprint, render_template
profile = Blueprint('profile', __name__, url_prefix='/<user_url_slug>')
@profile.url_defaults
def add_user_url_slug(endpoint, values):
    values.setdefault('user_url_slug', g.user_url_slug)
@profile.url_value_preprocessor
def pull_user_url_slug(endpoint, values):
    g.user_url_slug = values.pop('user_url_slug')
    query = User.query.filter_by(url_slug=g.user_url_slug)
    g.profile_owner = query.first_or_404()
@profile.route('/')
def timeline():
    return render_template('profile/timeline.html')
@profile.route('/photos')
def photos():
    return render_template('profile/photos.html')
@profile.route('/about')
def about():
    return render_template('profile/about.html')

方法:

  • dispatch_request(self): 匹配路由,返回视图函数或者错误处理函数的返回值,并且检测是否为option请求,如果是则构造默认的 ‘options response’ 响应。构造过程首先是 Request uri 所支持的方法集(get、post、等),然后更新 Responseallow 属性(set类型),最后返回Response对象,若不是option请求则执行视图函数;
  • make_response(self, rv): rv是视图函数的返回值,在python3中,rv可以使一个元组(body、status、headers)、Response类对象、 或者一个返回Response类对象的回调函数。这个函数的功能就是把视图函数返回的 status headers 绑定到 Response;
  • create_url_adapter(self, request):url_map 适配器。对werkzeug的Map的bindbind_to_environ两个方法进行了封装。bind: 绑定一个主机地址,并返回MapAdapter对象 ;bind_to_environ( 将MAP绑定到WSGI环境中,并返回MapAdapter对象(参数script_name在进行重定向时会用到);
  • try_trigger_before_first_request_functions(self):在该实例第一个请求到达时把当前线程加锁,然后依次执行被 before_first_request(self, f)装饰过得函数,然后释放锁并把_got_first_request置为True,再次就直接return;
  • preprocess_request(self) :该函数就是执行被url_value_preprocessorbefore_request装饰过的函数;
  • full_dispatch_request(self): 先执行try_trigger_before_first_request_functions,然后执行preprocess_request,若before_request中的函数有返回值则为其构造Response,然后跳过排在此函数后边的函数包括视图函数,若before_request的函数都返回的None或没有函数就执行 dispatch_request(self)
  • inject_url_defaults(self, error, endpoint, values): 执行被’url_defaults’ 装饰的函数
  • process_response(self, response): 同preprocess_request(self)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/home/fujiayu/esp/esp-idf/tools/check_python_dependencies.py:12: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html import pkg_resources Executing action: flash Running ninja in directory /home/fujiayu/esp/hello_world/build Executing "ninja flash"... [1/5] cd /home/fujiayu/esp/hello_world.../esp/hello_world/build/hello_world.bin hello_world.bin binary size 0x31ee0 bytes. Smallest app partition is 0x100000 bytes. 0xce120 bytes (80%) free. [1/1] cd /home/fujiayu/esp/hello_world..._world/build/bootloader/bootloader.bin Bootloader binary size 0x5290 bytes. 0x2d70 bytes (35%) free. [2/3] cd /home/fujiayu/esp/esp-idf/com...nents/esptool_py/run_serial_tool.cmake esptool esp32s3 -p /dev/ttyACM0 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 80m --flash_size 2MB 0x0 bootloader/bootloader.bin 0x10000 hello_world.bin 0x8000 partition_table/partition-table.bin esptool.py v4.6.2 Serial port /dev/ttyACM0 A fatal error occurred: Could not open /dev/ttyACM0, the port doesn't exist CMake Error at run_serial_tool.cmake:66 (message): /home/fujiayu/.espressif/python_env/idf5.2_py3.8_env/bin/python;;/home/fujiayu/esp/esp-idf/components/esptool_py/esptool/esptool.py;--chip;esp32s3 failed. FAILED: CMakeFiles/flash cd /home/fujiayu/esp/esp-idf/components/esptool_py && /usr/bin/cmake -D IDF_PATH=/home/fujiayu/esp/esp-idf -D "SERIAL_TOOL=/home/fujiayu/.espressif/python_env/idf5.2_py3.8_env/bin/python;;/home/fujiayu/esp/esp-idf/components/esptool_py/esptool/esptool.py;--chip;esp32s3" -D "SERIAL_TOOL_ARGS=--before=default_reset;--after=hard_reset;write_flash;@flash_args" -D WORKING_DIRECTORY=/home/fujiayu/esp/hello_world/build -P /home/fujiayu/esp/esp-idf/components/esptool_py/run_serial_tool.cmake ninja: build stopped: subcommand failed. ninja failed with exit code 1, output of the command is in the /home/fujiayu/esp/hello_world/build/log/idf_py_stderr_output_21690 and /home/fujiayu/esp/hello_world/build/log/idf_py_stdout_output_21690
07-07

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值