zoukankan      html  css  js  c++  java
  • 二、注册登录状态维持(template和session)

    from flask import Flask, render_template, session, request, redirect
    
    
    app = Flask(__name__)
    app.secret_key = "fafFAVGVvvbGbBfWFwvgWb"
    @app.route("/")
    def index():
        return "Hello World"
    
    
    #
    @app.route("/login", methods=["GET", "POST"])
    def login():
        if request.method == "GET":
            # ImmutableMultiDict([('name', 'Will'), ('name', 'Smith')])
            # 接收GET请求的参数
            # "GET /login?name=Will&name=Smith HTTP/1.1"
            print(request.args.get("name"))
            # Will
            print(request.args.getlist("name"))
            # ['Will', 'Smith']
            # 模板渲染,使用Jinjia2,
            return render_template("login.html")
    
        # ImmutableMultiDict([('username', 'admin')])
        # 接收POST请求的参数,可以直接使用get方法获取具体参数值
        print(request.form.get("name"))
        session["userInfo"] = {"username": "Will"}
        return redirect("/home")
    
    
    @app.route("/logout")
    def logout():
        del session["userInfo"]
        return redirect("/home")
    
    
    @app.route("/home")
    def home():
        userInfo = session.get("userInfo")
        print(userInfo)
        return render_template("home.html", **{"userInfo": userInfo})
    
    
    if __name__ == '__main__':
        app.run("localhost", 80, debug=True)

    1、template

    渲染模板的函数为render_template和render_template_string,传入渲染函数中的上下文必须使用下面两种形式(多采用第二种方式),与django不同。

    render_template("home.html", username="Will", age=20)
    
    render_template("home.html", **{"username":"Will", "age":20})

    模板文件默认放在当前目录下的templates子目录中,如果需要更改,可以在创建应用对象时,设置template_folder参数,将模板文件放在里面

    app = Flask(__name__, template_folder="templates")

    2、session

    flask的session跟django不同

    django是在cookie中设置一个sessionid,存放session的键,当浏览器发送请求,请求在到达视图函数时,会被相应的session中间键从数据库中根据sessionid取出当前请求用户的登录信息,这样就是可辨别是哪一个用户登录的。

    flask是在服务器中维护一个session,可以将其当做字典来使用,存储的值可以是任意的Python类型(包括字典、列表等),当一个请求到达时,就会将请求的cookie中的session信息存入服务器的session中,注意:flask的session信息是直接存储在客户端的,为了防止session信息泄露,必须要对session先序列化再加密,因此需要设置app.secret_key,可以随意设置,但是不得泄露。

    app = Flask(__name__)
    app.secret_key = "fafFAVGVvvbGbBfWFwvgWb"
    @app.route("/login", methods=["GET", "POST"])
    def login():
        if request.method == "GET":
            # ImmutableMultiDict([('name', 'Will'), ('name', 'Smith')])
            # 接收GET请求的参数
            # "GET /login?name=Will&name=Smith HTTP/1.1"
            print(request.args.get("name"))
            # Will
            print(request.args.getlist("name"))
            # ['Will', 'Smith']
            # 模板渲染,使用Jinjia2,
            return render_template("login.html")
    
        # ImmutableMultiDict([('username', 'admin')])
        # 接收POST请求的参数,可以直接使用get方法获取具体参数值
        print(request.form.get("name"))
        session["userInfo"] = {"username": "Will"}
        return redirect("/home")

    3、route

    flask使用装饰器与视图函数对应,如果app.route只传递rule字符串,那么只会默认通过GET请求,其他请求不会被允许通过,要是非GET请求通过,必须设置methods参数,

    @app.route("/login", methods=["GET", "POST"])

    视图函数是不用传request参的,因为都是直接导入的,

    from flask import Flask, render_template, session, request

    那如何接收GET、POST传递的参数呢?

    # ImmutableMultiDict([('name', 'Will'), ('name', 'Smith')])
    # 接收GET请求的参数
    # "GET /login?name=Will&name=Smith HTTP/1.1"
    print(request.args.get("name"))
    # Will
    print(request.args.getlist("name"))
    # ['Will', 'Smith']
    # ImmutableMultiDict([('username', 'admin')])
    # 接收POST请求的参数,可以直接使用get方法获取具体参数值
    print(request.form.get("name"))

    重定向?

    from flask import Flask redirect
    @app.route("/logout")
    def logout():
        del session["userInfo"]
        return redirect("/home")
  • 相关阅读:
    第五周总结
    第四周总结
    关于“模仿"和”创新“
    第三周总结
    第九周总结
    第八周总结
    第六周总结
    中国历史上成功的两人合作
    第五周总结
    第四周总结
  • 原文地址:https://www.cnblogs.com/loveprogramme/p/13366468.html
Copyright © 2011-2022 走看看