zoukankan      html  css  js  c++  java
  • flask之Flask特殊装饰器

    flask_decorators.py

      1 '''
      2 Flask中的特殊装饰器:
      3     (1)@app.before_request
      4         请求到达视图函数之前,进行自定义操作,类似django中间件中的process_request,在app中使用则为全局,在蓝图中使用则针对当前蓝图
      5         注意正常状态下return值必须为None
      6     (2)@app.after_request
      7         响应返回到达客户端之前,进行自定义操作,类似jango中间件中的process_response,在app中使用则为全局,在蓝图中使用则针对当前蓝图
      8         注意正常状态下视图函数必须定义一个形参接收response对象,并通过return response返回
      9     (3)@app.errorhandler()
     10         错误状态码捕获执行函数,装饰器参数务必是4xx或者5xx的int型错误状态码
     11     (4)@app.template_global() :定义装饰全局模板可用的函数,直接可在模板中进行渲染使用
     12        @app.template_filter(): 定义装饰全局模板可用的过滤器函数,类似django中的自定义过滤器,直接可在模板中使用
     13         两个特殊装饰器主要用在模板渲染,详情使用见falsk学习中的jinjia2学习
     14 '''
     15 import os
     16 
     17 from flask import Flask, render_template, session, redirect, request, send_file
     18 
     19 app = Flask(__name__)
     20 app.debug = True
     21 app.secret_key = 'sdfghjhg1234'
     22 
     23 
     24 # (1)@app.before_request请求达到视图函数之前装饰器函数,正常状态务必return None
     25 @app.before_request
     26 def b1():
     27     print('b1')
     28     urls = ['/login']
     29     if request.path in urls:
     30         return None
     31     if session.get('username'):
     32         return None
     33     else:
     34         return redirect('/login')
     35 
     36 
     37 @app.before_request
     38 def b2():
     39     print('b2')
     40     return None
     41 
     42 
     43 # (2)@app.after_request响应到达客户端之前装饰器函数,正常状态被装饰函数必须定义一个形参来接收response,务必return response
     44 @app.after_request
     45 def a1(response):
     46     print('a1')
     47     return response
     48 
     49 
     50 @app.after_request
     51 def a2(response):
     52     print('a2')
     53     return response
     54 
     55 
     56 # (3)@app.errorhandler(错误状态码)错误捕获装饰器,装饰其中必须传入4xx或5xx的错误状态码,同时在被装饰函数中定义一个形参来接收错误信息error
     57 @app.errorhandler(404)
     58 def notfond(errormessage):
     59     print(errormessage)
     60     return send_file(os.path.join(os.path.dirname(__file__), 'static', '1.png'))
     61 
     62 
     63 # (4)@app.template_global()和@app.template_filter()装饰器函数直接在模板中可以全局使用
     64 
     65 @app.template_global()
     66 def sum1(a, b):
     67     return a + b
     68 
     69 
     70 @app.template_filter()
     71 def sum2(a, b, c, d):
     72     return a + b + c + d
     73 
     74 
     75 # (5)@app.route()路由视图装饰器,第一个参数为请求路径,其它关键字参数使用相见flask之route路由学习
     76 # 可以使用app.add_url_rule(rule, endpoint=None, view_func=None,)进行路由
     77 @app.route('/login', methods=['GET', 'POST'])
     78 def login():
     79     if request.method == 'GET':
     80         print('login_get')
     81         return render_template('login.html')
     82     elif request.method == 'POST':
     83         print('login_post')
     84         username = request.form.get('username')
     85         pwd = request.form.get('pwd')
     86         if username == 'yang' and pwd == '123456':
     87             session['username'] = username
     88             return redirect('/index')
     89         else:
     90             return '登录失败!!!'
     91 
     92 
     93 @app.route('/index')
     94 def index():
     95     print('index')
     96     return render_template('index.html')
     97 
     98 
     99 @app.route('/data')
    100 def data():
    101     print('data')
    102     return 'data'
    103 
    104 
    105 @app.route('/detail')
    106 def detail():
    107     print('detail')
    108     return 'detail'
    109 
    110 
    111 if __name__ == '__main__':
    112     app.run()

    index.html

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>index</title>
     6 </head>
     7 <body>
     8 index页面
     9 </body>
    10 </html>
  • 相关阅读:
    iOS万能跳转界面的方法
    CocoaPods版本更新
    iOS--开发小技巧(持续更新)
    RunTime--手势应用场景(很方便)
    牛逼的标签效果(封装好)
    直播点赞动画
    UI基础--自定义UISwitch
    StatusBar 更改状态栏颜色(ios7)
    ios版本更新提示
    IOS 两个UIImage 合成一个Image
  • 原文地址:https://www.cnblogs.com/open-yang/p/11173223.html
Copyright © 2011-2022 走看看