表单验证评估为False Flask

时间:2019-03-09 18:32:13

标签: python flask flask-wtforms

我的登录表单与此非常相似,它就像一个超级按钮。我就是做不到 了解为什么form.validate_on_submit()给出False。如您所见,我正在使用print(form.errors)来查看正在发生的事情,但是我得到的只是空字典,并不是很有用。

此注册表存在问题:

@users.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        form = RegisterView(request.form)
        if form.validate_on_submit():
            user = User(
                first_name=form.first_name.data.title(),
                last_name=form.last_name.data.title(),
                email=form.email.data,
                mobile_number=form.mobile_number.data,
                city=form.city.data.title(),
                country=form.country.data,
                password=User.set_password(form.password.data),
            )
            db.session.add(user)
            db.session.commit()
            flash('Your account has been created. You can now log in.', 'success')
            return redirect(url_for('users.login'))
        else:
            print(form.errors)
    else:
        form = RegisterView()
    return render_template('users/register.html', title='Register User', \
                        form=form)

这是html:

<div class='container'>
    <form action='' method='post'>
        {{ form.hidden_tag() }}
        <legend>Register User</legend>
        <fieldset class='form-group'>
            <div class='form-group'>
            {{ form.first_name(class='form-control', placeholder='First Name') }}
            {{ form.last_name(class='form-control', placeholder='Last Name') }}
            {{ form.email(class='form-control', placeholder='@') }}
            {{ form.mobile_number(class='form-control', placeholder='Mobile Number') }}
            {{ form.city(class='form-control', placeholder='City') }}
            {{ form.country(class='form-control') }}
            {{ form.password(class='form-control', placeholder='Password') }}
            {{ form.conf_password(class='form-control', placeholder='Confirm Password') }}
            </div>
        </fieldset>
        <div class='form-group'>
        {{ form.submit(class='btn btn-primary') }}&nbsp;
        <a href='{{ url_for("main.register") }}' id='btn' class='btn btn-warning'>Back</a>
    </div>
</form>

更多详细信息:

class Configuration:
    APPLICATION_DIR = os.path.dirname(os.path.realpath(__file__))
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'xyz'
    MAIL_SUBJECT_PREFIX = 'xyz'
    MAIL_SENDER = 'vtc@gmail.com'  # change this to use ProtonMail instead

    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(Configuration):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = 'xyz'

0 个答案:

没有答案