如何在flaskwtforms中显示来自输入端的消息

2024-05-14 22:31:33 发布

您现在位置:Python中文网/ 问答频道 /正文

在这个脚本中,我将wtforms.validatorInputRequired的消息设置为显示错误。另外,我给出了字符长度的max和min。但这些论点并不奏效

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, IntegerField, SubmitField,StringField
from wtforms.validators import ValidationError, Length, EqualTo, InputRequired, Email

class Login(FlaskForm):
    email =StringField(validators=[InputRequired(message = 'please input something'), Length(min=10, max=20), Email(message='this is not email')])
    password = PasswordField(validators=[InputRequired(message='please input something'), Length(min=8, max=20, message='you must be input more than 8 character')])
    submit = SubmitField('Enter')

class Register(FlaskForm):
    email =StringField(validators=[InputRequired(message = 'please input something'), Length(min=10, max=20), Email(message='this is not email')])
    phone = IntegerField(validators=[InputRequired(message='please input something')])
    password = PasswordField(validators=[InputRequired(message='please input something'), Length(min=8, max=20, message='you must be input more than 8 character')])
    submit = SubmitField('Enter')

这个脚本是login.html

{% extends "base.html" %}
{% block content %}
<h3>this is Home page</h3>
<form action="#">
    <!-- {{forlogin.hidden_tag()}} -->
    {{forlogin.email(placeholder="email")}}
    <br>
    {{forlogin.password(placeholder="password")}}
    <br>
    {{forlogin.submit}}
</form>
{% endblock content %}

Tags: frommessageinputemailpasswordminlengthmax
1条回答
网友
1楼 · 发布于 2024-05-14 22:31:33

从WTForms文档中的字段类文档:

errors

If validate encounters any errors, they will be inserted into this list.

因此,要显示错误,您必须在页面上显示列表中的项目:

   <form action="#">
        {{login.email(placeholder="email")}}{% for error in login.email.errors %} {{ error }} {% endfor%}
   </form>

我不知道for在两个大括号之间意味着什么,我把它去掉了。它导致了金甲2的错误

相关问题 更多 >

    热门问题