AttributeError:'EditForm'对象没有属性'validate_on_submit' [英] AttributeError: 'EditForm' object has no attribute 'validate_on_submit'
问题描述
<来自flask.ext.wtf的form.py
从wtforms中导入表格
import Form,TextField,BooleanField,PasswordField,TextAreaField,验证器$ b $ from wtforms.validators import必需
$ b $ class EditForm(Form):
编辑用户配置文件EditForm
username = TextField('username',[validators.Length(min = 3,max = 50),validators.Required()])
email = TextField('email',[validators.Length电子邮件,validators.Required()])
密码= PasswordField('密码',[validators.Required()])
年龄= TextField('年龄',[validators.Length(min = 1,max = 3),validators.Required()])
about_user = TextAreaField('about_user',[validators.Length(max = 500)])
img_url = TextField ('img_url')
views.py
pre $
from flask import render_template,url_for,flash,g,redirect,session,request $ b $ from flask.ext.login import login_user,logout_user,current_user ,login_required
从表单导入LoginForm,RegisterForm,EditForm
@ app.route('/ edit',methods = ['GET','POST'])
def edit ():
form = EditForm(request.form)
$ b $ if request.method =='POST'and form.validate_on_submit():
g.user.username = form .data.username
g.user.email = form.data.email
g.user.age = form.data.age
g.user.img_url = form.data.img_url
g.user.about_user = form.data.about_user
db.session.add(g.user)
db.session.commit()
flash('Your changes saved successfully ')
return redirect(url_for('edit'))
else:
form.data.username = g.use r.username
form.data.email = g.user.email
form.data.age = g.user.age
form.data.img_url = g.user.img_url
form.data.about_user = g.user.about_user
return render_template('edit.html',
title ='Edit Profile',
form = form)
edit.html
{%extendsbase.html%}
{%block content%}
< div>
< form action =method =postname =edit>
< table>
< tr>
< td>使用者名称< / td>
< td> {{form.username}}< / td>
< / tr>
< tr>
< td>电子邮件< / td>
< td> {{form.email}}< / td>
< / tr>
< tr>
< td>年龄< / td>
< td> {{form.age}}< / td>
< / tr>
< tr>
< td>选择头像< / td>
< td> {{form.img_url}}< / td>
< / tr>
< tr>
< td>关于我< / td>
< td> {{form.about_user(cols = 50,rows = 5)}}< / td>
< / tr>
< tr>
< td>< / td>
< td>< input type =submitname =submitvalue =Save changes/>< / td> < / TR>
< / table>
< / form>
< / div>
{%endblock%}
错误的 Form 对象:
from flask.ext.wtf import Form从wtforms
导入Form,TextField,BooleanField,PasswordField,TextAreaField,验证器
第二个导入从 另外两个注释: 表单将从请求中获取值,不需要传入 以下就足够了: I have a small edit app with the files bellow. When I submit tbe form it shows me AttributeError: 'EditForm' object has no attribute 'validate_on_submit' Can anyone please tell me what is the issue? forms.py views.py edit.html
You imported the wrong The second import line imports Two additional notes: The form will take values from the request for you, no need to pass in The following then is enough:
这篇关于AttributeError:'EditForm'对象没有属性'validate_on_submit'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! wtforms
从 flask_wtf
替换导入 Form 。从第二个导入行中删除
Form ,并将您的
flask.ext.wtf
导入更新为 flask_wtf
保持未来 - )
$ $ $ $ $ $ $ $ $ $ $ $ $'$ $ $ $ $ $'验证器
request.form
。
validate_on_submit()
也可以测试请求方法,您自己也不需要这样做。
@app。如果form.validate_on_submit()函数返回true,则返回true,否则返回false。 ):
from flask.ext.wtf import Form
from wtforms import Form, TextField, BooleanField, PasswordField, TextAreaField, validators
from wtforms.validators import Required
class EditForm(Form):
"""edit user Profile EditForm"""
username = TextField('username', [validators.Length(min=3, max=50), validators.Required()])
email = TextField('email', [validators.Length(min=5, max=100), validators.Email, validators.Required()])
password = PasswordField('password', [validators.Required()])
age = TextField('age', [validators.Length(min=1, max=3), validators.Required()])
about_user = TextAreaField('about_user', [validators.Length(max=500)])
img_url = TextField('img_url')
from flask import render_template, url_for, flash, g, redirect, session, request
from flask.ext.login import login_user, logout_user, current_user, login_required
from forms import LoginForm, RegisterForm, EditForm
@app.route('/edit', methods = ['GET', 'POST'])
def edit():
form = EditForm(request.form)
if request.method == 'POST' and form.validate_on_submit():
g.user.username = form.data.username
g.user.email = form.data.email
g.user.age = form.data.age
g.user.img_url = form.data.img_url
g.user.about_user = form.data.about_user
db.session.add(g.user)
db.session.commit()
flash('Your changes saved successfully')
return redirect(url_for('edit'))
else:
form.data.username = g.user.username
form.data.email = g.user.email
form.data.age = g.user.age
form.data.img_url = g.user.img_url
form.data.about_user = g.user.about_user
return render_template('edit.html',
title = 'Edit Profile',
form = form)
{% extends "base.html" %}
{% block content %}
<div>
<form action="" method="post" name="edit">
<table>
<tr>
<td>Username</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>E-mail</td>
<td>{{ form.email }}</td>
</tr>
<tr>
<td>Age</td>
<td>{{ form.age }}</td>
</tr>
<tr>
<td>Choose Avatar</td>
<td>{{ form.img_url }}</td>
</tr>
<tr>
<td>About me</td>
<td>{{ form.about_user(cols = 50, rows = 5) }}</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Save changes"/></td> </tr>
</table>
</form>
</div>
{% endblock %}
Form
object:from flask.ext.wtf import Form
from wtforms import Form, TextField, BooleanField, PasswordField, TextAreaField, validators
Form
from wtforms
, replacing the import from flask_wtf
. Remove Form
from the second import line (and update your flask.ext.wtf
import to flask_wtf
to remain future-proof):from flask_wtf import Form
from wtforms import TextField, BooleanField, PasswordField, TextAreaField, validators
request.form
.validate_on_submit()
tests for the request method too, no need to do so yourself.@app.route('/edit', methods = ['GET', 'POST'])
def edit():
form = EditForm()
if form.validate_on_submit():