flask:TypeError:'tuple'对象不可调用

时间:2018-03-20 04:32:09

标签: python flask

我正在尝试使用在线教程学习烧瓶,当我尝试使用以下formpayload数据注册用户时,

csrf_token=ImJkOTlkYmVlMWQ2N2MwZGVmMGQ5MTRkZmZhODBmY2E0NWFiYTY4Y2Ei.DZIZ_w.UTdQ_hkzHXoIFS8oH8D84AMWLF8&username=vector&email=vcks%40xyz.com&password=1234&password2=1234&submit=Register

当我点击提交时,我得到跟踪追溯

  File "/Users/vaibhavchauhan/microblog/app/routes.py", line 51, in register
    if form.validate_on_submit():
  File "/Users/vaibhavchauhan/.pyenv/versions/3.6.2/envs/flasky/lib/python3.6/site-packages/flask_wtf/form.py", line 101, in validate_on_submit
    return self.is_submitted() and self.validate()
  File "/Users/vaibhavchauhan/.pyenv/versions/3.6.2/envs/flasky/lib/python3.6/site-packages/wtforms/form.py", line 310, in validate
    return super(Form, self).validate(extra)
  File "/Users/vaibhavchauhan/.pyenv/versions/3.6.2/envs/flasky/lib/python3.6/site-packages/wtforms/form.py", line 152, in validate
    if not field.validate(self, extra):
  File "/Users/vaibhavchauhan/.pyenv/versions/3.6.2/envs/flasky/lib/python3.6/site-packages/wtforms/fields/core.py", line 204, in validate
    stop_validation = self._run_validation_chain(form, chain)
  File "/Users/vaibhavchauhan/.pyenv/versions/3.6.2/envs/flasky/lib/python3.6/site-packages/wtforms/fields/core.py", line 224, in _run_validation_chain
    validator(form, self)
TypeError: 'tuple' object is not callable

@app.route('/register', methods=['GET','POST'])
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.data, email=form.email.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash('Congratulations, you are now a registered user!')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)

根据要求我正在为注册表添加代码。

class RegistrationForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    password2 = PasswordField(
        'Repeat Password', validators=[(DataRequired(), EqualTo('password'))])
    submit = SubmitField('Register')

    def validate_username(self, username):
        user = User.query.filter_by(username=username.data).first()
        if user is not None:
            raise ValidationError('please use diffrent username.')

    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user is not None:
            raise ValidationError('please use diffrent email.')

1 个答案:

答案 0 :(得分:1)

问题在于:

password2 = PasswordField(
    'Repeat Password', validators=[(DataRequired(), EqualTo('password'))])

validators应该是验证器列表。但相反,您将传递一个包含两个验证器元组的列表。解决它:

password2 = PasswordField(
    'Repeat Password', validators=[DataRequired(), EqualTo('password')])

validate()内,它执行类似(简化伪代码)的操作:

for field in fields:
    for validator in field.validators:
        validator(field)

因此,validator字段的第一个password2最终会作为两个函数的元组而不是函数,并且您无法调用元组。