config.py
### 数据库配置
DB_URI = 'postgresql://postgres:123456@127.0.0.1/testdb2'
SQLALCHEMY_DATABASE_URI = DB_URI
# 动态追踪修改设置,如未设置只会提示警告
SQLALCHEMY_TRACK_MODIFICATIONS=False
#查询时会显示原始SQL语句
SQLALCHEMY_ECHO = True
app.py
from flask import Flask,flash
from flask import url_for,render_template
from flask_sqlalchemy import SQLAlchemy#导入SQLAlchemy模块
from forms import BaseLogin
import config
app = Flask(__name__)
app.config.from_object(config)
db = SQLAlchemy(app)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(debug=True)
models.py
# -*- coding: utf-8 -*-
from werkzeug.security import generate_password_hash,check_password_hash
from app import db #导入db 对象
class User(db.Model):#定义User类
__tablename__='bmo_user'#表的别名
id = db.Column(db.Integer,primary_key=True,autoincrement=True)#定义id字段
username = db.Column(db.String(50),nullable=False)#定义username字段
password = db.Column(db.String(100),nullable=False)#定义password字段
telephone = db.Column(db.String(11), nullable=True)#定义telephone字段
gender = db.Column(db.String(11), nullable=True)
if __name__ == '__main__':
db.create_all()