Post 数据后无法重定向是什么原因?也不报错。

Henglu
  • 24

表单类:

from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,BooleanField,SubmitField
from wtforms.validators import DataRequired,Length

class LoginForm(FlaskForm):
    username = StringField('用户:',validators=[DataRequired()])
    password = PasswordField('密码:',validators=[DataRequired(),Length(8,128)])
    remember = BooleanField('记住')
    submit = SubmitField('验证')

APP路由:

from flask import Flask,render_template,redirect,url_for,flash
from demos.form.forms import LoginForm


app = Flask(__name__)
app.config['SECRET_KEY'] = 'fasdfas2342'
app.secret_key = 'secret string'

@app.route('/')
def index():
    return "Hello Flask"

@app.route('/login',methods=['GET','POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        username = form.username.data
        flash("welcome home , %s" % username)
        print(username)
        return redirect(url_for('index'))
    return render_template('login.html' , form=form)

模板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陆-Henglu</title>
    <link  rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/login.css') }}" >
    <script  type="text/javascript" src="{{ url_for('static',filename='jQuery/jquery-3.3.1.js') }}"></script>
    <script  type="text/javascript" src="{{ url_for('static',filename='js/login.js') }}"></script>
</head>
<body>
    <div id="body">
        <div id="logo">
            <img src="{{ url_for('static',filename='img/logo.jpg') }}">
        </div>
        <div id="form">
            <form method="post">
                {{ form.csrf_token }}
                {{ form.username.label }}  {{ form.username }}<br>
                {{ form.password.label }}  {{ form.password }}<br><br>
                {{ form.remember }}  {{form.remember.label }}<br><br>
                {{ form.submit }}<br>
            </form>
        </div>
    </div>
</body>
</html>

Post提交数据后无法重定向,也不报错?

回复
阅读 2.2k
3 个回答
二十一
  • 1.4k

感觉没毛病呀

form很明显缺一个action属性啊,post到哪都没有指定

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏