繁体   English   中英

Flask 登录表单提交后登录重定向 bask 登录

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM