![](/img/trans.png)
[英]WTF form.validate_on_submit() is not working while updating a form
[英]form.validate_on_submit() from flask_wtf FlaskForm not working
我正在嘗試使用 wtforms 提交表單。 我無法驗證。 我也嘗試使用{{ form.csrf_token }}
但它仍然沒有用。
app.config['SECRET_KEY'] = 'thisisthesecretkey'
class LoginForm(FlaskForm):
username = StringField('username', validators=[
InputRequired(), Length(min=4, max=15)])
password = PasswordField('password', validators=[
InputRequired(), Length(min=8, max=80)])
remember_me = BooleanField('remember me')
@app.route('/login', methods=['GET', 'POST'])
def login():
form = forms.LoginForm()
if form.validate_on_submit():
return 'worked'
return render_template('login.html', form=form)
我的金賈:
<form class="form-signin" action="/login" method="POST">
{{ form.hidden_tag() }}
{{ wtf.form_field(form.username) }}
{{ wtf.form_field(form.password) }}
{{ wtf.form_field(form.remember_me) }}
</form>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<p class="mt-5 mb-3 text-muted text-center">© 2020</p>
您的action
決定了表單將發布到/login
。 我想你想要action="/register"
我有一個 and 在表單之前,我把它們放在里面,然后在表單字段中添加了一個,這解決了這個問題,現在看起來像這樣:
<form class="form-signin-heading" method="POST" action="/login">
<div class="text-center mb-4">
<img class="mb-4" src="/static/logo.jpg" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Heading1</h1>
</div>
<div class="container">
{{ form.hidden_tag() }}
{{ wtf.quick_form(form) }}
<button class="btn btn-lg btn-primary btn-block" type="submit">Log in</button>
</div>
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.