@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
"""
flash('Login requested for user {}, remember_me={}'.format(
form.username.data, form.remember_me.data))
"""
# Checks if user name and password-hash are the same in the db (Log in). Else it will show error message
if user_name_exist(form.username.data) and get_password(form.username.data) == \
md5(form.password.data.encode()).hexdigest():
user = User.query.get(form.username.data)
login_user(user, remember=form.remember_me.data)
flash(current_user.is_authenticated)
return redirect(url_for('index'))
else:
form.showerror.text = "Password or User Name Incorrect"
return render_template('login.html', title='Login', form=form)
I confirm that the user is logged in by using the current_user.is_authenticated function, which is returning True. But i still cannot get to pages that i have the @login_required on them. What am I doing wrong?
@app.route('/index')
@login_required
def index():
form = ShowRoomsForm()
return render_template('index.html', title='Home', form=form)
EDIT: (Code for login manager)
login = LoginManager(app)
login.login_view = 'login'
I was able to fix this. The problem was my user loader function was getting the user through the user name and not id. i changed it to being by id and the problem was solved Here is the fix:
@login.user_loader
def user_loader(user_id):
user = User.query.filter_by(id=user_id).first()
if user:
return user
return None
I was able to fix this. The problem was my user loader function was getting the user through the user name and not id. i changed it to being by id and the problem was solved Here is the fix:
@login.user_loader
def user_loader(user_id):
user = User.query.filter_by(id=user_id).first()
if user:
return user
return None
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.