我的目标是实现:一旦在/login页面上使用正确的凭据登录,用户就会被重定向到/camera页面。
实际结果:/login 页面刚刚重新加载,表单输入已擦除。
无错误消息/异常
我已经查看了代码,但无法理解它为什么要这样做。任何帮助赞赏。
main.py:
from flask import Flask, render_template, redirect, url_for
from flask_wtf import FlaskForm
from flask_login import LoginManager, login_required
from flaskconfig import Config
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired
from werkzeug.security import check_password_hash
app = Flask(__name__)
app.config.from_object(Config)
logindatabase = {
'User1': 'pbkdf2:sha256:150000$QLfB2gVe$fd76b36df43686e7a8e27a9066331727e246f0d2835688d8d303af2f9b13a1cc',
'User2': 'pbkdf2:sha256:150000$4rK2hvN8$d4940c3dac1ce6f293bc495934fce92cb840c470c83eb0e3b864f407eec85868'
}
class LoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
submit = SubmitField('Sign In')
@app.route('/')
def home():
return redirect(url_for('login'))
@app.route('/login', methods = ['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = form.username.data
if user is None or user not in logindatabase.keys() or check_password_hash(logindatabase[user], form.password.data) == False:
return redirect(url_for('login'))
else:
login_user(user)
return redirect(url_for('camera'))
return render_template('login.html', title = 'Log In', form = form)
@app.route('/camera')
@login_required
def camera():
return render_template('camera.html')
if __name__ == '__main__':
app.run(host = '0.0.0.0')
注意:在主机 0.0.0.0 上运行,因为我在 repl.it 上开发(有意)
模板>登录.html:
<!DOCTYPE html>
<html>
<head>
<title>Login Page - Door Camera System</title>
</head>
<body>
<h1>Welcome!</h1>
<hr>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for msg in messages %}
<li>
{{ msg }}
</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
MYYA
相关分类