TypeError:' RegistrationForm'对象不可调用

时间:2017-03-25 02:43:10

标签: python

早晨费拉斯!

我在使用Flask表格时遇到了一些麻烦。这是代码(python2.7):

views.py

    # third-parties imports
    from flask import flash, redirect, render_template, url_for
    from flask_login import login_required, login_user, logout_user

    # local imports
    from . import auth
    from forms import LoginForm, RegistrationForm
    from .. import db
    from ..models import Empregado

    @auth.route('/register', methods=['GET', 'POST'])
    def register():
        form = RegistrationForm()
        if form.validate_on_submit():
            empr = Empregado(email=form.email.data,
                   username=form.username.data,
                   first_name=form.first_name.data,
                   last_name=form.last_name.data,
                   password=form.password.data)
            db.session.add(empr)
            db.session.commit()
            flash('Registration complete! Go ahead and Login!')

            # done registration. Heads for login
            return redirect(url_for('auth.login'))

        # When FLASK_DEBUG = 1, it yells a TypeError on the following line
        return render_template('auth/register.html', form=form('utf8'), title='Register')

forms.py

Altough RegistrationForm不可调用,LoginForm在auth / login.html上呈现,因此上面的代码片段只关注auth / register.html上的错误

# -*- coding: utf-8 -*-

# app/auth/forms.py

from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField, SubmitField, ValidationError
from wtforms.validators import DataRequired, Email, EqualTo
from ..models import Empregado

class RegistrationForm(FlaskForm):

    email = StringField('Email', validators=[DataRequired(), Email()])
    username = StringField('User', validators=[DataRequired()])
    first_name = StringField('First name', validators=[DataRequired()])
    last_name = StringField('Last name', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired(), EqualTo('retype')])
    retype = PasswordField('Retype Password')
    submit = SubmitField('submit')

def validate_email(self, field):
    if Empregado.query.filter_by(email=field.data).first():
        raise ValidationError('Email already used')

def validate_username(self, field):
    if Empregado.query.filter_by(username=field.data).first():
        raise ValidationError('User already exists')

class LoginForm(FlaskForm):

    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    submit = SubmitField('Login') 

回溯

File "~/Sources/py2/business_masters/app/auth/views.py", line 40, in register

return render_template('auth/register.html', form=form('utf8'), title='Register')

TypeError: 'RegistrationForm' object is not callable

我已经仔细检查过拼写错误和目录,但一切似乎都很好......我坚持这个(我的第一个网站,所以请不要在这个可怜的开发者身上努力... )。我感谢你的理解和时间![/ p>

1 个答案:

答案 0 :(得分:0)

form=form('utf8')表示您将对象传递给form而不是实例。

尝试使用return render_template('register.html', form=form)

以下是一个示例,请查看您的代码并与您的RegistrationForm进行比较:

from wtforms import Form, StringField, validators

class RegistrationForm(Form):
    username = StringField('Username', [validators.Length(min=4, max=25)])
    email = StringField('Email Address', [validators.Length(min=6, max=35)])

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegistrationForm()

    if request.method == 'POST' and form.validate():
        user = User(form.username.data, form.email.data,
                    form.password.data)
        db_session.add(user)
        flash('Thanks for registering')
        return redirect(url_for('login'))
    return render_template('register.html', form=form)

register.html:

<form class="form form-horizontal" method="post" role="form">
    {{ form1.hidden_tag() }}
    {{ wtf.form_errors(form1, hiddens="only") }}
    {{ wtf.form_field(form1.email,form_type="horizontal") }}
    {{ wtf.form_field(form1.username,form_type="horizontal") }}
    {{ wtf.form_field(form1.first_name,form_type="horizontal") }}
    {{ wtf.form_field(form1.last_name,form_type="horizontal") }}

  <div class="col-sm-offset-2 col-sm-10">
     <button name="action_save1" type="submit" class="btn btn-primary">submit</button>
  </div>
</form>

查看Form Validation with WTForms的更多详情。