1.表结构
DROP TABLE IF EXISTS `notice`;
CREATE TABLE `notice` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL DEFAULT '' COMMENT '标题',
`summary` varchar(10000) NOT NULL DEFAULT '' COMMENT '描述',
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态 1:有效 0:无效',
`view_count` int(11) NOT NULL DEFAULT '0' COMMENT '总浏览次数',
`updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后插入时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='公告表';
2.映射
source imooc/bin/activate
cd /home/www/order/
flask-sqlacodegen 'mysql://root:[email protected]/food_db' --tables notice --outfile "common/models/member/Notice.py" --flask
在生成的对象文件里
order/common/models/member/Notice.py
把
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
改成:
from application import db, app
完整文件如下
# coding: utf-8
from sqlalchemy import Column, DateTime, Integer, String
from sqlalchemy.schema import FetchedValue
from application import db, app
class Notice(db.Model):
__tablename__ = 'notice'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue())
summary = db.Column(db.String(10000), nullable=False, server_default=db.FetchedValue())
status = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
view_count = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
在
order\web\templates\common\layout_main.html
添加
<li class="notice">
<a href="{
{ buildUrl('/notice/index') }}"><i class="fa fa-group fa-lg"></i> <span
class="nav-label">公告列表</span></a>
</li>
C:\Users\huang\vincent\code\mooc\order\web\static\js\common.js
if( pathname.indexOf("/notice") > -1 ){
nav_name = "notice";
}
C:\Users\huang\vincent\code\mooc\order\www.py 添加
from web.controllers.member.Notice import route_notice
和
app.register_blueprint(route_notice, url_prefix="/notice")