from flask import Flask,request,views from functools import wraps app = Flask(__name__) #自定义登录装饰器 def login_required(func): @wraps(func) def wapper(*args,**kwargs): username = request.args.get('username') if username and username == 'xiaowu': return func(*args,**kwargs) else: return '请先登录' return wapper @app.route('/') def index(): return '网站首页' @app.route('/setting/') @login_required def setting(): return '这是设置页面' #类视图怎么使用装饰器 class ProfileView(views.View): decorators = [login_required] def dispatch_request(self): return "个人中心页面" app.add_url_rule('/profile/',endpoint='profile',view_func=ProfileView.as_view( 'profile' )) if __name__ == '__main__': app.run(debug=True)