首页 > Flask应用分层 MVC?

Flask应用分层 MVC?

当项目内容越来越多的时候,你会引入Flask的blueprint来分解你的业务逻辑。

本人是java开发转python的。
那么当遇到一些应用内的公共服务接口,可以给各个views来调用的情况。
这种情况在flask里面如何构建呢?

比如一些公用的数据库查询的逻辑。
在Java里面会有Service层,提供服务。

但是flask的结构貌似是按照blueprint来做的。
所以不知道怎么分解比较好??
求高手指点。


参考:https://exploreflask.com/blueprints.html
两种组织Blueprints 的方法。
1, 功能型

yourapp/
    __init__.py
    static/
    templates/
        home/
        control_panel/
        admin/
    views/
        __init__.py
        home.py
        control_panel.py
        admin.py
    models.py

2, 划分型

yourapp/
    __init__.py
    admin/
        __init__.py
        views.py
        static/
        templates/
    home/
        __init__.py
        views.py
        static/
        templates/
    control_panel/
        __init__.py
        views.py
        static/
        templates/
    models.py

每个单独的功能或模块可以放在一个views 文件或包里,公共服务接口可以放在上层位置,或作为单独的Blueprint (API 服务)或模块包。
根据项目的复杂度和功能特征来定。


或许你需要

exploreflask


我觉得公共服务接口可以写成API,整个API可以写成一个或多个blueprint
具体blueprint的划分可以按职能、模块来划分。比如如果需要登录模块,就可以把这个模块写成一个blueprint;如果需要用户资料模块,就可以把这个写成一个blueprint

【热门文章】
【热门文章】