嘿伙计们我正在使用Sanic和WTForms以及Sanic-WTF。我有这个代码用于我的SignUpForm:
class SignUpForm(SanicForm):
email = EmailField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired(),
Length(min=3, max=25), EqualTo('confirm_password', message='Passwords must match.')])
confirm_password = PasswordField('Confirm Password', validators=[DataRequired()])
我也将此作为我在基于类的视图中验证的验证。
async def post(self, request):
form = SignUpForm(request)
email = form.email.data
print(email)
email_exists = await unique_db(email)
print(form.errors)
print ('Form validated: {}, Email Exists: {}'.format(form.validate_on_submit(), email_exists))
if form.validate_on_submit() and email_exists == False:
print('Validated')
await app.db.user_details.update_one({'user':'details'},{'$set': data}, upsert=True)
return json({'success':True})
return template('signup.html', form=form)
我不知道发生了什么事。我也试过form.validate()
我的SignUp表单是
<form name="SignupForm" style="padding-top: 50px;" method="post">
{{ form.csrf_token }}
<div class="row justify-content-center">
<div class="col-lg-8 col-md-10 col-sm-12">
<div class="card">
<div class="card-body">
<h3 class="card-title">Sign up</h3>
{{ macros.render_field(form.email, label_visible=true, placeholder='Email', type='email') }}
<small id="passwordHelpBlock" class="form-text text-muted">Your email must be in the form name@example.com</small>
{{ macros.render_field(form.password, label_visible=true, placeholder='Password', type='password') }}
{{ macros.render_field(form.confirm_password, label_visible=true, placeholder='Confirm Password', type='password') }}
<button type="submit" class="btn btn-success" id="login-submit-button">Submit</button>
</div>
<div class="card-footer text-muted text-center">
Already have an account? <a href="/login">Log in</a>
</div>
</div>
</div>
</div>
</form>
答案 0 :(得分:0)
好吧所以他们似乎是SanicForm中的一个错误,因为当我将我的SignUpForm类改为继承自Form(通用WTForm表单类)时,它运行良好。