标签:rom hash beans web route class sel elf info
from werkzeug.security import generate_password_hash,check_password_hash
#book模型,传入db核心对象把Book插入db
class User(UserMixin,Base):
__tablename__ = ‘user‘
id = Column(Integer,primary_key=True)
nickname = Column(String(24), nullable=False)
phone_number = Column(String(18), unique=True)
_password=Column("password",String(228), nullable=False)
email = Column(String(50), unique=True, nullable=False)
confirmed = Column(Boolean, default=False)
beans = Column(Float, default=0)
send_counter = Column(Integer, default=0)
receive_counter = Column(Integer, default=0)
wx_open_id =Column(String(50))
wx_name =Column(String(32))
@property
def password(self):
return self._password
@password.setter
def password(self,raw):
self._password=generate_password_hash(raw)
def check_password(self,raw):
return check_password_hash(self._password,raw)
装饰器原理: https://www.cnblogs.com/kaibindirver/p/12892064.html
视图函数
@web.route(‘/register‘, methods=[‘GET‘, ‘POST‘])
def register():
form=RegisterForm(request.form)
if request.method =="POST" and form.validate():
user=User()
user.set_attrs(form.data)
db.session.add(user)
db.session.commit()
return redirect(url_for("web.login"))
return render_template("auth/register.html",form=form)
@web.route(‘/login‘, methods=[‘GET‘, ‘POST‘])
def login():
form=LoginForm(request.form)
if request.method =="POST" and form.validate():
user= User.query.filter_by(email=form.email.data).first()
if user and user.check_password(form.password.data):
login_user(user)
flash("登录成功")
else:
flash("账号不存在或密码错误")
return render_template("auth/login.html",form=form)
标签:rom hash beans web route class sel elf info
原文地址:https://www.cnblogs.com/kaibindirver/p/12945369.html