Albumy 登录视图显示账号被封禁


#1
from flask import render_template, flash, redirect, url_for, Blueprint
from flask_login import login_user, logout_user, login_required, current_user, login_fresh, confirm_login

from myalbumy.emails import send_confirm_email, send_reset_password_email
from myalbumy.extensions import db
from myalbumy.forms.auth import LoginForm, RegisterForm, ForgetPasswordForm, ResetPasswordForm
from myalbumy.models import User
from myalbumy.settings import Operations
from myalbumy.utils import generate_token, validate_token, redirect_back

auth_bp = Blueprint('auth', __name__)


@auth_bp.route('/login', methods=['GET', 'POST'])
def login():
    """
    视图:处理登录请求
    """
    if current_user.is_authenticated:
        return redirect(url_for('main.index'))

    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data.lower()).first()
        if user is not None and user.validate_password(form.password.data):
            if login_user(user, form.remember_me.data):
                flash('成功登录!', 'info')
                return redirect_back()
            else:
                flash('您的账号已被封禁!', 'warning')
                return redirect(url_for('main.index'))
        flash('无效的邮箱或密码!', 'warning')
    return render_template('auth/login.html', form=form)

账号密码都正确的,为什么一直显示“您的账号已被封禁!”??


#2

这个用户是新创建的吗?可以打印 user.active 看看输出。(后续有问题在论坛创建新帖子即可,微信不适合交流代码问题 :sweat_smile:


#3

好的!用户是管理员admin@helloflask.com啊?


#4
        if user is not None and user.validate_password(form.password.data):
            if login_user(user, form.remember_me.data):
                flash('成功登录!', 'info')
                return redirect_back()
            else:
                print(user.active)
                flash('您的账号已被封禁!', 'warning')
                return redirect(url_for('main.index'))
        flash('无效的邮箱或密码!', 'warning')

后台显示False


#5
127.0.0.1 - - [23/Feb/2019 22:00:00] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Feb/2019 22:00:07] "GET /auth/login HTTP/1.1" 200 -
127.0.0.1 - - [23/Feb/2019 22:00:23] "POST /auth/login HTTP/1.1" 302 -
False
127.0.0.1 - - [23/Feb/2019 22:00:23] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Feb/2019 22:06:47] "GET /auth/login HTTP/1.1" 200 -
False
127.0.0.1 - - [23/Feb/2019 22:07:00] "POST /auth/login HTTP/1.1" 302 -
127.0.0.1 - - [23/Feb/2019 22:07:00] "GET / HTTP/1.1" 200 -

#6

低级错误:在定义User模型时,把active属性默认值为False了!!还以为login_user存在问题呢!!


#7

发一下 models.py 里 User 模型类的字段定义看看。


#8

:sweat: