[英]Flask Login redirects bask to login after Login form is submitted
我正在創建一個對用戶進行身份驗證的 web 應用程序。 在 Heroku 上部署站點並測試功能后,一切運行良好。 我可以像往常一樣注冊用戶並登錄。 一天后,我突然不能再這樣做了。 單擊登錄按鈕后,我被重定向回登錄頁面。
以下是我在 Heroku 上獲得的日志:
2021-06-01T19:53:56.781147+00:00 heroku[router]: at=info method=GET path="/static/main.css" host=webapp.herokuapp.com request_id=a30ccbd8-cc0e-4829-8b8e-8f5d41984fb4 fwd="62.8.95.69" dyno=web.1 connect=0ms service=5ms status=200 bytes=1577 protocol=https
2021-06-01T19:53:56.782359+00:00 app[web.1]: 10.113.128.161 - - [01/Jun/2021:19:53:56 +0000] "GET /static/main.css HTTP/1.1" 200 0 "https://webapp.herokuapp.com/register" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
2021-06-01T19:54:17.372766+00:00 heroku[router]: at=info method=GET path="/login?next=%2F" host=webapp.herokuapp.com request_id=877d3a88-80ad-4415-b3ef-75eaf2d86f15 fwd="154.122.170.227" dyno=web.1 connect=2ms service=4ms status=200 bytes=4725 protocol=https
2021-06-01T19:54:17.373646+00:00 app[web.1]: 10.47.129.242 - - [01/Jun/2021:19:54:17 +0000] "GET /login?next=%2F HTTP/1.1" 200 4570 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"
2021-06-01T19:54:18.314233+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=webapp.herokuapp.com request_id=bceb62ba-e855-4adb-bdad-5be328bd8f18 fwd="154.122.170.227" dyno=web.1 connect=5ms service=2ms status=404 bytes=3032 protocol=https
2021-06-01T19:54:18.315665+00:00 app[web.1]: 10.47.129.242 - - [01/Jun/2021:19:54:18 +0000] "GET /favicon.ico HTTP/1.1" 404 2870 "https://webapp.herokuapp.com/login?next=%2F" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"
2021-06-01T19:54:28.863138+00:00 app[web.1]: 10.183.123.207 - - [01/Jun/2021:19:54:28 +0000] "POST /login?next=%2F HTTP/1.1" 200 4597 "https://webapp.herokuapp.com/login?next=%2F" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"
2021-06-01T19:54:28.863227+00:00 heroku[router]: at=info method=POST path="/login?next=%2F" host=webapp.herokuapp.com request_id=54ad91c3-7330-464a-be41-3019a4472b17 fwd="154.122.170.227" dyno=web.1 connect=0ms service=4ms status=200 bytes=4752 protocol=https
2021-06-01T19:54:29.523285+00:00 app[web.1]: 10.183.123.207 - - [01/Jun/2021:19:54:29 +0000] "GET /favicon.ico HTTP/1.1" 404 2870 "https://webapp.herokuapp.com/login?next=%2F" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"
2021-06-01T19:54:29.523299+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=webapp.herokuapp.com request_id=bae1353a-4a9e-407c-9df9-afad92a7f9a6 fwd="154.122.170.227" dyno=web.1 connect=0ms service=3ms status=404 bytes=3032 protocol=https
我無法弄清楚問題是什么。
這是我的登錄和注冊路線:
@users.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('main.home'))
form = LoginForm()
# Validate submitted data
if form.validate_on_submit():
# Query DB to get user email & get first email
user = User.query.filter_by(email=form.email.data).first()
# Check that User exists and password in DB matches form password
if user and bcrypt.check_password_hash(user.password, form.password.data):
login_user(user, remember=form.remember.data)
next_page = request.args.get('next')
return redirect(next_page) if next_page else redirect(url_for('main.home'))
else:
flash('Login Unsuccessful. Please check email & password', 'danger')
return render_template('login.html', title='Login', form=form)
@users.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('main.home'))
form = RegistrationForm()
# Validate submitted data
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
user = User(username=form.username.data, email=form.email.data, password=hashed_password, usertype=form.usertype.data)
db.session.add(user)
db.session.commit()
flash('Your account has been created successfully!\nYou are now able to log in', 'success')
return redirect(url_for('users.login'))
return render_template('register.html', title='Register', form=form)
我不明白是什么導致了這個問題。
嘗試將您的 Procfile 更改為:
web: gunicorn app:app --preload
請注意,app:app 可能會有所不同,具體取決於您命名文件的方式。 只需在 Procfile 中所有內容的末尾添加--preload
即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.