Skip to content

Validation of WTForms in Flask using Postman

I am trying to Create a user using WTForms. But always I am getting validation error when checked using POSTMAN. I am not sure why this happens

users = Blueprint('users', __name__)
@users.route("/register", methods=['GET', 'POST'])
def register():
    if current_user.is_authenticated:
        return jsonify({"user":"already signed in"})
    form = CreateUserForm(request.form)
    if form.validate():
        hashed_password = bcrypt.generate_password_hash('utf-8')
        user = User(,, password=hashed_password,
              , activation_code=form.activation_code.form)
        flash('Your account has been created! You are now able to log in', 'success')
        return jsonify({"message":"user auth success"})
    return jsonify({"message":"validation failed"})

class CreateUserForm(FlaskForm):
    username = StringField('username',
                           validators=[DataRequired(), Length(min=2, max=20)])
    email = StringField('email',
                        validators=[DataRequired(), Email()])
    password = PasswordField('password', validators=[DataRequired()])
    confirm_password = PasswordField('confirm_password',
                                     validators=[DataRequired(), EqualTo('password')])
    role = StringField('role',
    activation_code = StringField('activation_code',
    # submit = SubmitField('Sign Up')
    def validate_username(self, username):
        user = User.query.filter_by(
        if user:
            raise ValidationError('That username is taken. Please choose a different one.')
    def validate_email(self, email):
        user = User.query.filter_by(
        if user:
            raise ValidationError('That email is taken. Please choose a different one.')

class User(db.Model, UserMixin):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20))
    password = db.Column(db.String(255))
    email = db.Column(db.String(100), unique = True)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    role = db.Column(db.String(6), default = 'Member')
    activation_code = db.Column(db.String(255), default = None)
    rememberme = db.Column(db.String(255))
    reset = db.Column(db.String(255), default = None)
    registered = db.Column(db.String(15), default = time_format())
    lastseen = db.Column(db.String(15), default = time_format(), onupdate = time_format())
    tfa_code = db.Column(db.String(255), default = None)
    ip = db.Column(db.String(255), default = None)
    def __init__(self, username, password, email, role, activation_code):
        self.username = username
        self.set_password(password) = email
        self.role = role
        self.activation_code = activation_code

Postman screen shot

enter image description here

what can be the reason for validation always failing in this case. Thanks in advance


For this to work from POSTMAN, set the config