AttributeError: 'User' object has no attribute 'set_attrs'
来源:9-9 ORM的方式保存模型
SuccessorSocialism
2018-04-08
写回答
3回答
-
你有在基类里添加这个方法吗?我课程应该是没有遗漏的啊。
052018-04-10 -
jefflike
2018-04-08
这个。
112018-04-10 -
SuccessorSocialism
提问者
2018-04-10
base.py: from flask_sqlalchemy import SQLAlchemy from sqlalchemy import Column, SmallInteger, Integer db = SQLAlchemy() class Base(db.Model): __abstract__ = True # create_time = Column('create_time',Integer) status = Column(SmallInteger,default=1 ) def set_attrs(self,attrs_dict): for key,value in attrs_dict.items(): if hasattr(self,key) and key != 'id': setattr(self,key,value) auth.py: from app.forms.auth import RegisterForm ,LoginForm from app.models.base import db from . import web from app.models.user import User from flask import render_template, redirect, request, url_for, flash from werkzeug.security import generate_password_hash @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() redirect(url_for('web.login')) # user.password = generate_password_hash(form.password.data) # user.nickname = form.nickname.data # request.form return render_template('auth/register.html',form = form)
062018-04-11
相似问题