根据《Flask入门教程》改编的登陆程序,点击登陆按钮后报错:AttributeError: 'NoneType' object has no attribute 'account'

期望的行为:点击后能登陆到系统界面
实际行为:点击后触发500错误
Pyhton版本:Python 3.7
软件版本:PyCharm Professional 2019.1.1
尝试过的解决方法:1.将uaccount = request.form[‘account’]改为uaccount=request.form[str(‘account’)] 结果:无济于事

相关代码块:
(a)视图函数代码

@app.route('/login',methods=['GET', 'POST'])
def sign_in():
    if request.method == 'POST':
        uaccount = request.form['account']
        password = request.form['password']

        if not uaccount or not password:
            flash('不合法输入:用户名或密码中有一项为空!')
            return redirect(url_for('sign_in'))

        user = User.query.first()   #验证用户名和密码是否正确

        if uaccount == user.account and password == user.password:
            login_user(user)
            flash('欢迎光临!')
            return redirect(url_for('index'))
        flash('用户名或密码错误!')#验证失败返回错误提示
        return redirect(url_for('sign_in'))
    return render_template('login.html')

(b)涉及的类

class User(db.Model, UserMixin):  # 表名将会是 user(自动生成,小写处理),用户
    id = db.Column(db.Integer, primary_key=True)  # 主键
    account = db.Column(db.String(20),unique=True)   #账号,不可重复
    username = db.Column(db.String(20))  # 姓名
    password = db.Column(db.String(20))   #密码
    identity = db.Column(db.String(20),unique=True)  #身份证号

© 主要HTML页面代码

<form class="form-signin" method="post">
        <h2 class="form-signin-heading" align="center">用户登录</h2><br>
        <h4>这里输入账号:</h4><label for="inputAccount" class="sr-only">输入账号</label>
        <input type="text" name="account" id="inputAccount" class="form-control" placeholder="Account" required autofocus>
        <h4>这里输入密码:</h4><label for="inputPassword" class="sr-only">输入密码</label>
        <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
        <div class="checkbox">
          <label>
            <input type="checkbox" value="remember-me"> 记住我
          </label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="submit">登录</button>
      </form>

错误信息输出:

[2019-05-24 20:21:38,863] ERROR in app: Exception on /login [POST]
Traceback (most recent call last):
  File "C:\Users\xsing\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\xsing\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\xsing\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\xsing\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\xsing\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\xsing\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\xsing\PycharmProjects\ANCRS\app.py", line 103, in sign_in
    if uaccount == user.account and password == user.password:
AttributeError: 'NoneType' object has no attribute 'account'
127.0.0.1 - - [24/May/2019 20:21:38] "POST /login HTTP/1.1" 500 -

你没有创建用户记录,所以 User.query.first() 调用会返回 None。你可以在这一行后面添加一个判断,比如:

user = User.query.first()

if not user:
    flash('管理员账户未创建。')
    return redirect(url_for('sign_in'))
1 个赞

十分感谢,真是一针见血,药到病除

1 个赞