猿问

为什么 Flask Python 网页上的“提交”按钮重定向到相同的 HTML 模板

我的目标是实现:一旦在/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 %}



繁星coding
浏览 66回答 1
1回答

MYYA

方法应为开机自检并删除新潮。<form action="" method="post">&nbsp; &nbsp; {{ form.hidden_tag() }}&nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; {{ form.username.label }}<br>&nbsp; &nbsp; &nbsp; &nbsp; {{ form.username(size=32) }}&nbsp; &nbsp; </p>&nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; {{ form.password.label }}<br>&nbsp; &nbsp; &nbsp; &nbsp; {{ form.password(size=32) }}&nbsp; &nbsp; </p>&nbsp; &nbsp; <p>{{ form.submit() }}</p>&nbsp; &nbsp; {{ form.csrf_token }}</form>
随时随地看视频慕课网APP

相关分类

Python
我要回答