文章目录
项目目录规划参考方案:
mkdir LaiKe
cd LaiKe/
# server(server端目录)
mkdir -p project/server
touch project/server/models.py
touch project/server/settings.py
# client(client端目录)
mkdir -p project/client
mkdir -p project/client/static
mkdir -p project/client/templates
t
##### main 模块 ########
# server -> main
mkdir -p project/server/main
touch project/server/main/__init__.py
touch project/server/main/views.py
# client > main
mkdir -p project/client/templates/main
touch -p project/client/templastes/main/home.html
####### User 模块 #######
# server -> user
mkdir -p project/server/user
mkdir -p project/server/user/__init__.py
mkdir -p project/server/user/forms.py
mkdir -p project/server/user/views.py
# client -> user
mkdir -p project/client/templates/user
touch -p project/client/templastes/user/login.html
touch -p project/client/templastes/user/register.html
touch -p project/client/templastes/user/members.html
######## entry模块 ##########
#server -> entry (词条)
mkdir -p project/server/entry/
touch project/server/entry/__init__.py
touch project/server/entry/forms.py
touch project/server/entry/views.py
# client -> entry
mkdir -p project/client/templates/entry
touch -p project/client/templastes/entry/login.html
touch -p project/client/templastes/entry/register.html
touch -p project/client/templastes/entry/members.html
目录结构总结
-
app的创建:初始化的时,可以通过template_folder指定模板位置,通过static_folder指定静态资源位置
-
蓝图的构建:通过Blueprint为每个模块定义一个蓝图对象,然后通过app.register_blueprint将各个模块通过蓝图对象集成到app中;
-
插件的应用:一个插件通常就是一个类,插件其实就是对flask创建的app的包装,即把app当做构造方法的参数。例如:
toolbar = DebugToolbarExtension(app) bootstrap = Bootstrap(app) login_manager = LoginManager(app) db = SQLAlchemy(app) # ORM插件
-
Model的设计:ORM
-
Views的设计:Flask中的Views包含路由和视图函数。设计风格:RESTful 风格、非RESTful风格、前后端分离接口风格、模板风格。
Models设计
import datetime
from project.server import db, bcrypt, app
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(255), unique=True, nullable=False)
password = db.Column(db.String(255), nullable=False)
registered_on = db.Column(db.DateTime, nullable=False)
admin = db.Column(db.Boolean, nullable=False, default=False)
entries = db.relationship('Entry', backref='creator')
def __init__(self, email, password, admin=False):
self.email = email
self.password = bcrypt.generate_password_hash(
password, app.