zoukankan      html  css  js  c++  java
  • flask基础四 请求上下文()

    内容回顾:

    1.Flask-Session
            from flask import session
            from flask_session import Session
            
            app.config["SESSTION_TPYE"] = "redis"
            app.config["SESSTION_REDIS"] = Redis("ip",port,db=1)
            # select 1  ----- [1]
            
            Session(app)
            
            session["key"] = "value"
            session["key"]
            
        2.Flask CBV
            class Index(views.MethodView):
                methods = ["GET"]
                decorators = [a,b,c]
                def get():
                    return "file"
                    
            
            app.add_url_rule("/",endpoint="index",view_func=Index.as_view(name="index"))
            
            if 1 : endpoint = None = name = "index"
            elif 2: endpoint="index" != name="index123"
        
        3.Flask WTForms
            from wtforms import Form,validators,widgets
            from wtforms.fields import simple
            class LgoinForm(Form):
                username = simple.StringField(
                    label = "用户名",
                    validators=[
                        validators.DataRequired(message="用户名不能为空"),
                    ],
                    widget = widgets.TextInput(),
                    render_kw = {"class":"jinwangba"}
                )
            
            def a():
                "GET":
                return render_template("a.html",fm = LgoinForm())
                "POST":
                request.form
                lfm = LgoinForm(request.form)
                lfm.validate() true false
            
            {{ fm.username.label }} {{ fm.username }} {{ fm.username.errors.0 }}

    flask回顾:

    # 1.路由@app.route('/',methods=["GET","POST"],endpoint="helloWorld",strict_slashes=True)
        # 动态路由参数 /<arg> def viewfunc(arg)
        # url_for
        # 2.三贱客:
        # HTTPResponse return 'Hello World!'
        # render retrun render_template("index.html")
        # redirect retrun redirect("/login")

        # 3.request
        # 存放数据:
        # request.form # 从表单 FormData 获取到的数据
        # request.args # URL传参时 获取到的数据
        # request.json # Content-Type:application/json 获取数据
        # request.data # b"" Content-Type:xiaowangba 获取数据
        # 请求属性的
        # request.method = "GET"/"POST"
        # request.path = "/index"
        # request.url = "http://127.0.0.1:5000/index"
        # 坑
        # request.values.to_dict() 同名被GET(args)覆盖

        # 4.session : Flask-Session
        # app.secret_key = "加密字符串"
        # 存放在浏览器Cookies中的被序列化后的session
        # session["key"] = "value"
        # session["key"]
        # session 默认31天的生命周期

        # 5.Blueprint
        # 蓝图对象 可以理解为 一个不可以被启动的Flask对象
        # bp = Blueprint("bluename",__name__,template_folder="temp_blue")
        # @bp.route("/blue")
        # app.register_blueprint(blueprint.bp)

        # 6.Flask配置
        # 1.Flask对象实例配置
        # class Obj(object):
        #     DEBUG = True
        #     SESSION_TYPE = "redis"
        #     SESSION_REDIS = Redis(db=5)
        # app.config.from_object(Obj)

        # 2.Flask初始化的配置
        # template_folder = 模板文件存放路径
        # static_folder = "静态文件存放路径"
        # static_url_path = "/静态文件URL访问地址"默认是static_folder


        # 7.Flask Jinja2
        # {{  }} 当引用变量 和 执行函数的时候 , 非逻辑引用
        # {% %} if for 逻辑代码引用
        # obj.name obj.get("name") obj["name"]  obj_list.1  obj_list[1]
        # Markup  | safe
        # @app.template_global()
        # @app.template_filter()

        # 8.before_request after_request errorhandler(404,500)
        # @app.before_request 多个before_request依次执行
        # def be1():
        #     return None

        # @app.after_request 多个after_request,反序执行
        # def af1(response)
        #     return response

        # 正常情况: be1-be2-af2-af1
        # 异常情况: be1-af2-af1
        # @app.errorhandler(404) 重定义错误信息

        # 9.flash
        # flash("be_fr1")
        # get_flashed_messages()


        # 10.send_file jsonify
        # send_file("文件路径") 打开并返回文件内容
        # jsonify({k:v}) 将字典序列化json,打包一个Content-Type:application/json 返回给客户端

        # 11.Flask CBV
        # class Index(views.MethodView):
        #     methods = ["GET"]
        #     decorators = [a,b,c]
        #     def get():
        #         return "file"
        #
        #
        # app.add_url_rule("/",endpoint="index",view_func=Index.as_view(name="index"))
        #
        # if 1 : endpoint = None = name = "index"
        # elif 2: endpoint="index" != name="index123"

    1.DBUtils 数据库连接池

    首先下载插件pip install DBUtils或者python3 -m pip install DBUtils,

    然后运行mysql数据库,在mysql新创建一个数据库,并创建一个新的表,录入数据。

    import pymysql
    from DBUtils.PooledDB import PooledDB
    POOL = PooledDB(
        creator=pymysql,  # 使用链接数据库的模块
        maxconnections=6,  # 连接池允许的最大连接数,0和None表示不限制连接数
        mincached=2,  # 初始化时,链接池中至少创建的空闲的链接,0表示不创建
        maxcached=5,  # 链接池中最多闲置的链接,0和None不限制
        maxshared=3,  # 链接池中最多共享的链接数量,0和None表示全部共享。PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。
        blocking=True,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
        maxusage=None,  # 一个链接最多被重复使用的次数,None表示无限制
        setsession=[],  # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."]
        ping=0,
        # ping MySQL服务端,检查是否服务可用。
        # 如:0 = None = never,
        # 1 = default = whenever it is requested,
        # 2 = when a cursor is created,
        # 4 = when a query is executed,
        # 7 = always
        host='127.0.0.1', # 主机
        port=3306, # 端口
        user='root', # 用户
        password='', # 密码
        database='s12day113',  # 创建的数据库的名字
        charset='utf8'
    )

    也可以查询单条信息:

    也可以插入一条数据:

    2.Flask请求上下文,应用上下文

     

  • 相关阅读:
    python的Collections 模块
    python模块
    python类
    python异常
    python文件处理
    python函数
    python字符串
    python数据结构
    python循环
    下载Google Play外国区APP技巧
  • 原文地址:https://www.cnblogs.com/yb635238477/p/9780899.html
Copyright © 2011-2022 走看看