Skip to content
Advertisement

I’m not able to import Flask-WTF TextField and BooleanField

I’m using virtualenv to set up a new project. I installed a lot of things using virtualenv pip from the script folder like below:

flaskscriptspip install Flask-WTF

I have no other packages installed in the global python folder. My code looks like this:

# 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)

and other packages are found like sqlalchemy also installed only in the virtual environment.

The error I get is:

(flask) D:Developmentgrading>flaskScriptspython.exe restserver.py Traceback (most recent call last):
File "restserver.py", line 1, in <module> from app import app
File "D:Developmentgradingapp__init__.py", line 12, in <module> from forms import LoginForm
File "D:Developmentgradingappforms.py", line 1, in <module> from flask.ext.wtf import Form, TextField, BooleanField
File "D:Developmentgradingflasklibsite-packagesflaskexthook.py", line 87, in load_module
raise ImportError('No module named %s' % fullname) ImportError: No module named flask.ext.wtf.TextField

Form is found but not TextField and BooleanField. What is the problem here?

Update I just looked through some of the Flask-WTF code and found this:

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

Am I using examples from an older version or something?

Advertisement

Answer

From version 0.9.0, Flask-WTF will not import anything from wtforms, you need to import fields from wtforms.

Source

You need to import them from wtforms (note that according to docs import statement was changed):

from flask_wtf import Form

from wtforms import TextField, BooleanField
from wtforms.validators import Required
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement