乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Flask后端實(shí)踐 連載十二 Flask優(yōu)雅的注冊(cè)藍(lán)圖及自定義MethodView

       看見(jiàn)就非常 2020-04-29

      tips:

      • 介紹如何有效的統(tǒng)一注冊(cè)藍(lán)圖及自定義MethodView
      • 本文基于python3編寫(xiě)
      • 代碼倉(cāng)庫(kù)

      目的

      在項(xiàng)目開(kāi)發(fā)中,通常采用工程模式來(lái)創(chuàng)建app,如果注冊(cè)接口寫(xiě)在工廠(chǎng)函數(shù)中,不僅不好管理,而且代碼也看起來(lái)很臃腫。并且項(xiàng)目中會(huì)有很多模塊,每個(gè)模塊又有不同的功能,由此分割出來(lái)的接口也非常多。所有需要有統(tǒng)一的地方來(lái)管理接口及相關(guān)模塊。

      解決方法

      1. 編寫(xiě)藍(lán)圖(user.py)

        from flask import Blueprint
        
        bp = Blueprint("test", __name__, url_prefix='/')
        
        
        @bp.route('/testBP', methods=["GET"])
        def test_bp():
            return "藍(lán)圖測(cè)試"
        
        
      2. 編寫(xiě)自定義MethodView(auth.py)

        from flask.views import MethodView
        
        class AuthMethodView(MethodView):
            # 指定需要啟用的請(qǐng)求方法
            __methods__ = ["GET", "POST", "PUT"]
        
            def get(self):
                return "測(cè)試自定義MethodView"
        
            def post(self):
                return "測(cè)試自定義MethodView"
        
            def put(self):
                return "測(cè)試自定義MethodView"
        
            def delete(self):
                return "測(cè)試自定義MethodView"    
        
      3. 統(tǒng)一管理藍(lán)圖和自定義MethodView(router.py)

        from user import bp as user_bp
        from auth import AuthMethodView
        
        router = [
            user_bp, # 用戶(hù)藍(lán)圖接口
            AuthMethodView, # 權(quán)限自定義MethodView
        ]
        
      4. 統(tǒng)一注冊(cè)藍(lán)圖和自定義MethodView(app.py)

        from flask import Flask, Blueprint
        from router import router
        
        
        def create_app():
            """
            工廠(chǎng)模式創(chuàng)建APP
            """
            app = Flask(__name__)
            # 注冊(cè)接口
            register_api(app, router)
        
            return app
        
        
        def register_api(app, routers):
            """
            注冊(cè)藍(lán)圖和自定義MethodView
            """
            for router in routers:
                if isinstance(router, Blueprint):
                    app.register_blueprint(router)
                else:
                    try:
                        endpoint = router.__name__
                        view_func = router.as_view(endpoint)
                        # url默認(rèn)為類(lèi)名小寫(xiě)
                        url = '/{}/'.format(router.__name__.lower())
                        if 'GET' in router.__methods__:
                            app.add_url_rule(url, defaults={'key': None}, view_func=view_func, methods=['GET', ])
                            app.add_url_rule('{}<string:key>'.format(url), view_func=view_func, methods=['GET', ])
                        if 'POST' in router.__methods__:
                            app.add_url_rule(url, view_func=view_func, methods=['POST', ])
                        if 'PUT' in router.__methods__:
                            app.add_url_rule('{}<string:key>'.format(url), view_func=view_func, methods=['PUT', ])
                        if 'DELETE' in router.__methods__:
                            app.add_url_rule('{}<string:key>'.format(url), view_func=view_func, methods=['DELETE', ])
                    except Exception as e:
                        raise ValueError(e)
        
        
        if __name__ == '__main__':
            app = create_app()
            app.run()
        
        
      5. 測(cè)試,啟動(dòng)app

      • 瀏覽器訪(fǎng)問(wèn)http://127.0.0.1:5000/testBP,頁(yè)面返回藍(lán)圖測(cè)試

      • 瀏覽器訪(fǎng)問(wèn)http://127.0.0.1:5000/authmethodview/ ,頁(yè)面返回測(cè)試自定義MethodView

      總結(jié)

      • 統(tǒng)一管理了藍(lán)圖和自定義的MethodView,并減少了代碼的冗余,保持代碼的整潔性。
      • 下一篇將介紹使用flask輸出excel報(bào)表

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多