Python build flask and orator framework development

First show my project directory structure:

 

 

 

 

The first step is to create a requirements.txt file

flask == 1.0.3 // Write version 
tzlocal == 1.5.1
pillow == 6.1.0
orator == 0.9.8
validate-email == 1.3
flask-paginate == 0.5.3
gunicorn == 19.9.0
pymysql == 0.9.3 The

second step
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple 

Install the extension package needed above

Step 3 Create the startup file run.py

from src import create_app 

app = create_app ()

if __name__ == '__main__':
app.run ()


here's create_app () self-created method mainly configures some files src __init__.py configuration file

# coding=utf-8

from flask import Flask, url_for, redirect
from src.config.setting import Config


def create_app():
app = Flask(__name__)
app.debug = True
app.config.from_object(Config)
  # app.add_template_global (lang, 'lang') // Add template global variables

# Hook (middleware)
# @ app.before_request
# def before_request ():
# print (1)

# blueprint (module)
from src.module.bio import bp_bio
app.register_blueprint (bp_bio, url_prefix = "/ bio")

# index
app.route @ ( '/')
DEF system_index ():
return redirect (url_for ( "bio.index_index")) # declared here the root of url_for not need to write the name of the route need to write a method name

return App


src the following main config below Write configuration file
# Database configuration file
class Config:
    # 密匙
SECRET_KEY = '123'

# 环境 development, production
ENV = 'development'

# 调试模式
DEBUG = True

# orator 数据库配置
ORATOR_DATABASES = {
'default': 'mysql',
'mysql': {
'driver': 'mysql',
'host': '127.0.0.1',
'database': 'member',
'user': 'root',
'password': '123123',
'prefix': ''
}
}

# 超级管理员
SYS_SUPERS = [1]

 创建数据库配置 #模型层 都需要引入 这个数据库配置
from flask import current_app
from orator import DatabaseManager


class Database:

@staticmethod
def db():
return DatabaseManager(current_app.config['ORATOR_DATABASES'])

模型层代码
先创建 base模型
from src.config.database import Database


class Base:
db = None

def __init__(self):
self.db = Database.db()

之后每个模型类继承当前的base模型类
接下来一步 就是创建 服务层

from src.model.member import Member as MemberModel


class MemberService:

@staticmethod
def get_list():
return MemberModel().get_member_list()

不需要任何处理 直接调用就好

接下来一步就是创建 控制器 这个 就是比较重要的一步
先在 bio下面的__init__.py注册 蓝图控制器
from flask import Blueprint

bp_bio = Blueprint("bio", __name__)

import src.module.bio.controller

在 src.moudle.bio.controller下面的__init__.py中 声明 你要 注册的 controller
import src.module.bio.controller.index #引入一下就可以了 
接下来 在 index.py写代码 就好了 也不需要创建class类
from flask import render_template
from src.service.member import MemberService

from src.module.bio import bp_bio


@bp_bio.route('/index', methods=['GET']) #声明路由
def index_index():
member_list = MemberService().get_list()
print(member_list)
return render_template('bio/index.html')

大概就搭建结束
接下来在命令行运行 python run.py文件即可
浏览器 访问 127.0.0.1:5000#默认端口 5000

 

 所有代码 运行成功




Guess you like

Origin www.cnblogs.com/jhcyzxx/p/12744619.html