我无法导入Flask-WTF TextField和BooleanField

时间:2013-08-18 08:10:20

标签: python import flask wtforms flask-wtforms

我正在使用virtualenv来设置一个新项目。我在脚本文件夹中使用virtualenv pip安装了很多东西,如下所示:

flask\scripts\pip install Flask-WTF

我没有在全局python文件夹中安装其他软件包。我的代码如下所示:

# Importing TextField and BooleanField is not working...
from flask.ext.wtf import Form, TextField, BooleanField
from flask.ext.wtf import Required


class LoginForm(Form):
    openid = TextField('openid', validators=[Required()])
    remember_me = BooleanField('remember_me', default=False)

和其他软件包一样,sqlalchemy也只安装在虚拟环境中。

我得到的错误是:

(flask) D:\Development\grading>flask\Scripts\python.exe restserver.py Traceback (most recent call last):
File "restserver.py", line 1, in <module> from app import app
File "D:\Development\grading\app\__init__.py", line 12, in <module> from forms import LoginForm
File "D:\Development\grading\app\forms.py", line 1, in <module> from flask.ext.wtf import Form, TextField, BooleanField
File "D:\Development\grading\flask\lib\site-packages\flask\exthook.py", line 87, in load_module
raise ImportError('No module named %s' % fullname) ImportError: No module named flask.ext.wtf.TextField

找到表单但不是TextField和BooleanField。这有什么问题?

更新 我只是浏览了一些Flask-WTF代码,发现了这个:

from flask.ext.wtf import Form
from wtforms.fields import TextField, BooleanField
from wtforms.validators import Required

我是否使用旧版本的示例或其他内容?

1 个答案:

答案 0 :(得分:32)

  

从版本0.9.0开始,Flask-WTF不会从wtforms导入任何内容,您需要从wtforms导入字段。

     

Source

您需要从wtforms导入它们(请注意,根据docs导入声明已更改):

from flask_wtf import Form

from wtforms import TextField, BooleanField
from wtforms.validators import Required