• Flask登录认证


    login函数

    @app.route('/login/', methods=['GET', 'POST'])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
        # POST
        user = request.form.get('user')
        pwd = request.form.get('pwd')
        if user == 'tom' and pwd == '123':
            session['user'] = user
            return redirect('/index/')
        else:
            return render_template('login.html', error='账号或密码错误')

    1、判断逻辑直接写在视图函数中(low)

    @app.route('/index/')
    def index():
        if session.get('user'):
            return render_template('index.html')
        return redirect('/login/')

    2、使用装饰器(适用于,需要登录较少的网站)

    def wrapper(func):
        @wraps(func)
        def inner(*args, **kwargs):
            if not session.get('user'):
                return redirect('/login/')
            ret = func(*args, **kwargs)
            return ret
        return inner
    @app.route('/index/')
    @wrapper
    def index():
        # if session.get('user'):
        #     return render_template('index.html')
        # return redirect('/login/')
        return render_template('index.html')

    3、befer_request ===>相当于Django中的process_request

    # -*- encoding: utf-8 -*-
    from flask import Flask, render_template, request, redirect, session
    from functools import wraps
    app = Flask(__name__)
    app.secret_key = 'abc'
    
    
    # def wrapper(func):
    #     @wraps(func)
    #     def inner(*args, **kwargs):
    #         if not session.get('user'):
    #             return redirect('/login/')
    #         ret = func(*args, **kwargs)
    #         return ret
    #     return inner
    @app.before_request
    def auth():
        if request.path == '/login/':
            return None
        if session.get('user'):
            return None
        else:
            return redirect('/login/')
    
    
    @app.route('/login/', methods=['GET', 'POST'])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
        # POST
        user = request.form.get('user')
        pwd = request.form.get('pwd')
        if user == 'tom' and pwd == '123':
            session['user'] = user
            return redirect('/index/')
        else:
            return render_template('login.html', error='账号或密码错误')
    
    
    @app.route('/test/')
    def test():
        return render_template('test.html')
    
    
    @app.route('/index/')
    def index():
        # if session.get('user'):
        #     return render_template('index.html')
        # return redirect('/login/')
        return render_template('index.html')
    #
    
    
    if __name__ == '__main__':
        app.run()
  • 相关阅读:
    navicat连接虚拟机中mysql"Access denied for user'root'@'IP地址'"问题
    Centos6.4 + mysql-5.6.38-linux-glibc2.12-x86_64.tar 实现mysql主从复制
    三、mock测试技术
    二、数据加密
    一.unittest框架初识
    3.Allure报告
    2.pytest参数化
    1.pytest框架初识
    RabbitMQ 几种工作模式---(三) Publish/Subscribe
    RabbitMQ 几种工作模式---(二)work
  • 原文地址:https://www.cnblogs.com/wt7018/p/11599279.html
Copyright © 2020-2023  润新知