TypeError:'RegistrationForm'对象不可调用(TypeError: 'RegistrationForm' object is not c

编程入门 行业动态 更新时间:2024-10-23 11:33:24
TypeError:'RegistrationForm'对象不可调用(TypeError: 'RegistrationForm' object is not callable)

早晨费拉斯!

我正面临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

我已经仔细检查了错别字和目录,但一切看起来都很好......我被困在这里(我的第一个网站,所以请不要在这个可怜的开发者身上努力工作......)。 我感谢你的理解和时间!

Morning Fellas!

I'm facing some troubles with a Flask form. Here is the code (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 is not callable, LoginForm is rendered on auth/login.html, so the snippet of code above only focus on the error on 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')

Traceback

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

I have double-checked for typos and directories, but everything seems fine... I'm stuck on this (my first website, so please, don't go hard on this poor dev...). I appreciate your comprehension and time fellas!

最满意答案

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>

使用WTForms查看来自表单验证的更多详细信息。

form=form('utf8') which means you're passing a class object to form not an instance.

Try to use return render_template('register.html', form=form).

Here is an example,check out your code and compare with your 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>

See more details from Form Validation with WTForms.

更多推荐

本文发布于:2023-08-05 00:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   RegistrationForm   TypeError   object   callable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!