@web.route('/my/gifts')
@login_required
def my_gifts():
return "my gifts"
@login_manager.user_loader
def get_user(self, uid):
# 如果是根据主键查询,不要filter_by,使用get方法即可
return User.query.get(int(uid))
login_manager.login_view = 'web.login'
login_manager.login_message = '请先登录或注册'
@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, remember=True)
# request.form 获取表单信息;
# request.args获取url路径?后面的信息
next = request.args.get('next')
# next.startswith('/')防止重定向攻击
if not next or not next.startswith('/'):
return redirect(url_for('web.index'))
return redirect(next)
else:
flash("账号不存在或者密码错误")
return render_template('auth/login.html', form=form)