zoukankan      html  css  js  c++  java
  • flask中路由,实例化配置,对象配置,特殊装饰器

    1.Flask 中的路由
    *endpoint - url_for 反向地址
    *endpoint 默认是视图函数名
    *methods 指定视图函数的请求方式,默认GET
    defaults={"nid":1} 指定视图函数的默认参数
    strict_slashes=False 是否严格遵循路由规则 /login/
    redirect_to="/login" 永久跳转地址 301

    *动态路由参数:
    /<int:nid> /<string:str> /<nid>
    视图函数中需要有参数接收动态路由参数

    2.Flask中的实例化配置
    *template_folder = "temp" # template模板目录, 默认当前项目中的 templates 目录
    *static_folder = "jingtaiwenjianmulu" 目录
    *static_url_path = "/static" 访问路径
    static_host =

    host_matching = False, # 如果不是特别需要的话,慎用,否则所有的route 都需要host=""的参数
    subdomain_matching = False, # 理论上来说是用来限制SERVER_NAME子域名的,但是目前还没有感觉出来区别在哪里
    instance_path = None, # 指向另一个Flask实例的路径
    instance_relative_config = False # 是否加载另一个实例的配置
    root_path = None # 主模块所在的目录的绝对路径,默认项目目录

    3.app对象配置
    app.config.from_object(Debug)
    class Debug(object):
    DEBUG=True

    4.蓝图 Blueprint
    form flask import Blueprint
    blue = Blueprint("blue_id",__name__,url_prefix)
    url_prefix 前缀
    app.register_blueprint(blue)

    5.特殊装饰器:
    @app.template_global()
    @app.template_filter()

    @app.before_request
    请求进入视图函数之前

    @app.after_request
    def af1(response)
    return response
    结束视图函数之后,返回客户端之前


    正常:be1 - be2 - be3 - af3 - af2 - af1
    异常:be1 - af3 - af2 - af1

    @app.errorhandler(404) 重定义页面
    def error404(args):

  • 相关阅读:
    init-method,@postcontruct,afterPropertiesSet的先后顺序
    读写分离与分库分表,分布式事务面试题
    innerHTML的HTML居然必须大写..不可思议
    postgres/greenplum unnest(Array) 实现列转行
    AWS EBS磁盘挂载和卸载
    当npm 与淘宝镜像cnpm运行都很慢时候
    IntersectionObserver API 之学习
    vue之队列过渡组效果,后进先出坑点
    ele之vue3.0的form表单验证与重置
    vue3.0之DOM的$refs之运用
  • 原文地址:https://www.cnblogs.com/wszxdzd/p/10140219.html
Copyright © 2011-2022 走看看