使用 Postman 在 Flask 中验证 WTForms

Validation of WTForms in Flask using Postman

我正在尝试使用 WTForms 创建用户。但在使用 POSTMAN 检查时,我总是收到验证错误。我不确定为什么会这样

routes.py

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(form.password.data).decode('utf-8')
        user = User(username=form.username.data, email=form.email.data, password=hashed_password, 
                        role=form.role.data, activation_code=form.activation_code.form)
        db.session.add(user)
        db.session.commit()
        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"})

forms.py

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',
                           validators=[DataRequired()])
    activation_code = StringField('activation_code',
                           validators=[DataRequired()])
    # submit = SubmitField('Sign Up')

    def validate_username(self, username):
        user = User.query.filter_by(username=username.data).first()
        if user:
            raise ValidationError('That username is taken. Please choose a different one.')

    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user:
            raise ValidationError('That email is taken. Please choose a different one.')

models.py

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)
        self.email = email
        self.role = role
        self.activation_code = activation_code

邮递员屏幕截图

在这种情况下,验证总是失败的原因可能是什么。提前致谢

要在 POSTMAN 中运行,请设置配置

WTF_CSRF_ENABLED = False