1、秘钥设置:
app.secret_key = '随意设置'


2、SQLALCHEMY配置:
# 连接数据库
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:@127.0.0.1:3306/数据库名'
# 动态追踪修改设置,如未设置只会提示警告
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# 查询时会显示原始SQL语句
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)

 

2、数据库迁移:
导包:
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
配置:
# 支持黑窗口 输入命令
manager = Manager(app)

# 将数据库迁移的命令加入到黑窗口中 作用是用于更新数据库的结构
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)


执行迁移命令

1.python 文件 db init

2.python 文件 db migrate -m"版本名(注释)"

3.python 文件 db upgrade 然后观察表结构

注意: 只有第一次生成迁移文件夹时候,才需要执行第一条命令。

修改程序入口

if __name__ == '__main__':

# app.run(debug=True)

manager.run()

 

4、闪现消息:
from flask import Flask, flash

页面弹窗:
{% for mes in get_flashed_messages() %}
<script>alert('{{ mes }}')</script>
{% endfor %}

4、菜单栏:

(1)、 header内容

<style>

ul{

list-style: none;

}


ul li{

display: block;

margin-right: 20px;

float: left;

}

</style>

(2)、body内容

<body>

<div style="float: left; width: 100%">

<ul>

<li><a href="路由接口">分类1</a></li>

<li><a href="路由接口">分类2</a></li>

<li><a href="路由接口">分类3</a></li>

<li><a href="路由接口">分类4</a></li>

<li><a href="路由接口">分类5</a></li>

</ul>

</div>

<hr >

<table style="float: left;" border="1">

<tr>

<td>序号</td>

<td>商品名称</td>

<td>价格</td>

</tr>


<tr>

<td>1</td>

<td>牛角面包</td>

<td>200.00</td>

</tr>


<tr>

<td>2</td>

<td>法式面包</td>

<td>100.00</td>

</tr>

</table>

</body>

 

6、日志配置
(1) 导包:
import logging
from logging.handlers import RotatingFileHandler
(2)配置:
#日志文件,作用:用来记录程序的运行过程,比如:调试信息,接口访问信息,异常信息
def log_file(level):
# 设置日志的记录等级,设置日志等级: 常见等级有:DEBUG < INFO < WARING < ERROR < FATAL(CRITICAL)
logging.basicConfig(level=level) # 调试debug级
# 创建日志记录器,指明日志保存的路径、每个日志文件的最大大小、保存的日志文件编号
file_log_handler = RotatingFileHandler("logs/log", maxBytes=1024*1024*100, backupCount=10)
# 创建日志记录的格式 日志等级 输入日志信息的文件名 行数 日志信息
formatter = logging.Formatter('%(levelname)s %(filename)s:%(lineno)d %(message)s')
# 为刚创建的日志记录器设置日志记录格式
file_log_handler.setFormatter(formatter)
# 为全局的日志工具对象(flask app使用的)添加日志记录器
logging.getLogger().addHandler(file_log_handler)


7、CSRF配置
(1)导包:
from flask_wtf import CSRFProtect
(2)配置:
CSRFProtect(app)
(3)表单配置:
<form method="post" action="/">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
</form>


8、项目配置文件
class Config(object):
"""工程配置信息"""
DEBUG = True
"""SQLAlchemy 配置"""
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:12345678@127.0.0.1:3306/imformation'
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_COMMIT_ON_TEARDOWN = True # 数据库内容发送改变之后,自动提交
# 查询时会显示原始SQL语句
# SQLALCHEMY_ECHO = True
SECRET_KEY = '1811'
LEVEL = 'DEBUG'

 

posted on 2019-04-24 20:34  满仟  阅读(132)  评论(0编辑  收藏  举报