我的目标是实现:一旦在/login页面上使用正确的凭据登录,用户就会被重定向到/camera页面。实际结果:/login 页面刚刚重新加载,表单输入已擦除。无错误消息/异常我已经查看了代码,但无法理解它为什么要这样做。任何帮助赞赏。main.py:from flask import Flask, render_template, redirect, url_forfrom flask_wtf import FlaskFormfrom flask_login import LoginManager, login_requiredfrom flaskconfig import Configfrom wtforms import StringField, PasswordField, SubmitFieldfrom wtforms.validators import DataRequiredfrom werkzeug.security import check_password_hashapp = 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_requireddef 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 %}
1 回答
MYYA
TA贡献1868条经验 获得超4个赞
方法应为开机自检并删除新潮。
<form action="" method="post">
{{ form.hidden_tag() }}
<p>
{{ form.username.label }}<br>
{{ form.username(size=32) }}
</p>
<p>
{{ form.password.label }}<br>
{{ form.password(size=32) }}
</p>
<p>{{ form.submit() }}</p>
{{ form.csrf_token }}
</form>
添加回答
举报
0/150
提交
取消