页面未在Flask中重定向

时间:2019-02-06 17:58:43

标签: python flask flask-wtforms

在我的Flask应用中,单击“提交”按钮页面后,它不会重定向。

return redirect(url_for('home')) 

main.py

from flask import Flask,render_template,flash,redirect,url_for
from forms import RegistrationForm

app = Flask(__name__)

@app.route('/')
@app.route('/home')
def home():
  return render_template("index.html")

@app.route('/show-form',methods=['GET','POST'])
def show():
   form = RegistrationForm()
   return render_template('register.html',form=form)

@app.route('/submit',methods=['GET','POST'])
def insert():
   form = RegistrationForm()
   if form.validate_on_submit():
      flash(f'Account created for {form.username.data}','success')
      return redirect(url_for('home'))

if __name__ == '__main__':
   app.run(debug = True)

forms.py

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

class RegistrationForm(FlaskForm):
  username = StringField('Username',validators=[DataRequired(),Length(min=2,max=8)])
  email = StringField('Email',validators=[DataRequired(),Email()])
  submit = SubmitField('Submit form')

0 个答案:

没有答案