簡體   English   中英

AttributeError:“ NoneType”對象沒有屬性“ password”

[英]AttributeError: 'NoneType' object has no attribute 'password'

我有一個燒瓶項目,我從5個月前開始,並在進行注冊和身份驗證后停止。 我現在想繼續,在全新安裝之后,我現在獲得登錄/身份驗證的標題錯誤。 下面

帳戶表格

class LoginForm(Form):
    email = StringField('Enter email', validators=[DataRequired(),Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    remember = BooleanField('Remember Password')

在路由文件中

@app.route('/',methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
    formLogin = AccountForm.LoginForm()
    if request.method == 'GET' :
        return render_template('index.html',formLogin=formLogin)
    if request.method == 'POST' :
        if request.form.get('login', None)  == 'Login' :
            return AccountController.authenticatePopUpLogin(formLogin,'index')

在我的帳戶控制器中

def authenticatePopUpLogin(formLogin,route):
    if formLogin.validate_on_submit():
        try:
            user = session.query(User).filter(User.email == formLogin.email.data).first()
 except :# models.DoesNotExist:
            flash("Your email or password does not match !", "error")
            return render_template('login.html',form=formLogin,formLogin = formLogin)
        else :
            if check_password_hash(user.password,formLogin.password.data):

我的用戶是從我的模型類中導入的

class User(UserMixin , Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    title = Column(CHAR(3), nullable = False)
    firstname = Column(String(100), nullable = False)
    lastname = Column(String(100), nullable = False)
    DateOfBirth = Column(ArrowType, default = arrow.utcnow())
    username = Column(String(100), nullable = False, unique = True)
    email = Column (String(50), nullable =False, unique = True)
    password = Column(String(100), nullable = False) 
    ...

然后,它引發上面的錯誤。 我懷疑錯誤發生在這里check_password_hash(user.password,formLogin.password.data): 我的表單驗證有效,但是在清空等情況時會引發錯誤。

我確認密碼字段也存在於我的數據庫中。 請問我哪里出錯了?

根據first一種方法的文檔:

返回此查詢的第一個結果,如果結果不包含任何行,則返回None

因此,您必須檢查user是否為None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM