WTForms:安装“ email_validator”以获得电子邮件验证支持


15

运行以下代码进行表单验证时获取异常。

File "/Users/homeduvvuri/Documents/Learning/PartyGoUdemy/PartGo/user/forms.py", line 11, in BaseUserForm
    email = EmailField('Email', [validators.DataRequired(), validators.Email()])
File "/Users/homeduvvuri/Documents/Learning/PartyGoUdemy/PartGo/partgo-env/lib/python3.7/site-packages/wtforms/validators.py", line 332, in __init__
    raise Exception("Install 'email_validator' for email validation support.")
Exception: Install 'email_validator' for email validation support.

在codeanywhere VM上完美运行。不在本地计算机上。

from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import Form, StringField, PasswordField, validators, ValidationError
from wtforms.validators import InputRequired, Email
from wtforms.fields.html5 import EmailField
from wtforms.widgets import TextArea
from user.models import User

class BaseUserForm(FlaskForm):
    name = StringField('Name', [validators.DataRequired(), validators.Length(min=2, max=30)])
    email = EmailField('Email', [validators.DataRequired(), validators.Email()])

堆栈跟踪可以使用某些格式。
Sid

Answers:


16

如果您看一下第9行的wtforms / validators.py文件:

import email_validator

只需安装软件包:

pip install email_validator

2
甚至例外也使解决方案变得清晰:Exception: Install 'email_validator' for email validation support.
dspencer

我不好的人。我很好奇为什么它在我的虚拟机上没有时在我的本地实例上明确要求这样做。从一开始就试图弄清楚如何避免和避免此类跨机器/平台错误。
VeeDuvv

2
就在今天,我遇到了同样的问题,该应用程序在本地运行良好,但在VM上引发了此错误。WTForms就在今天发布了新版本2.3.1,该版本已中断。回到2.2.1为我修复了它。
absk


3

如果您要安装它wtforms

pip install wtforms[email]
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.