gpt4 book ai didi

python - 使用 Flask 从 WTForm 进行电子邮件验证

转载 作者:太空狗 更新时间:2023-10-29 17:01:39 25 4
gpt4 key购买 nike

我正在学习来自 http://code.tutsplus.com/tutorials/intro-to-flask-adding-a-contact-page--net-28982 的 Flask 教程我目前停留在验证步骤:

旧版本有以下内容:

from flask.ext.wtf import Form, TextField, TextAreaField, SubmitField, validators, ValidationError

class ContactForm(Form):
name = TextField("Name", [validators.Required("Please enter your name.")])
email = TextField("Email", [validators.Required("Please enter your email address."), validators.Email("Please enter your email address.")])
submit = SubmitField("Send")

阅读评论后,我将其更新为:(将验证器替换为 Required 为 InputRequired)

(same fields)  

class ContactForm(Form):
name = TextField("Name", validators=[InputRequired('Please enter your name.')])
email = EmailField("Email", validators=[InputRequired("Please enter your email address.")]), validators.Email("Please enter your email address.")])
submit = SubmitField("Send")

我唯一的问题是我不知道如何处理验证器。电子邮件。我收到的错误消息是:

NameError: name 'validators' is not defined

我查看了文档,也许我没有深入研究,但我似乎找不到电子邮件验证的示例。

最佳答案

试试这个:

from flask.ext.wtf import Form
from wtforms import validators
from wtforms.fields.html5 import EmailField

class ContactForm(Form):
email = EmailField('Email address', [validators.DataRequired(), validators.Email()])

关于python - 使用 Flask 从 WTForm 进行电子邮件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25324113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com