AttributeError:'EditForm'对象没有属性'validate_on_submit' [英] AttributeError: 'EditForm' object has no attribute 'validate_on_submit'

查看:877
本文介绍了AttributeError:'EditForm'对象没有属性'validate_on_submit'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的编辑应用程序与下面的文件。当我提交表单时,它显示我 AttributeError:'EditForm'对象没有任何属性'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,验证器

第二个导入从 wtforms flask_wtf 替换导入 Form 。从第二个导入行中删除 Form ,并将您的 flask.ext.wtf 导入更新为 flask_wtf 保持未来 - )

$ $ $ $ $ $ $ $ $ $ $ $ $'$ $ $ $ $ $'验证器

另外两个注释:


  1. 表单将从请求中获取值,不需要传入 request.form


  2. validate_on_submit()也可以测试请求方法,您自己也不需要这样做。

  3. $ b

以下就足够了:

  @app。如果form.validate_on_submit()函数返回true,则返回true,否则返回false。 ):


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

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')

views.py

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)

edit.html

{% 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 %}

解决方案

You imported the wrong Form object:

from flask.ext.wtf import Form
from wtforms import Form, TextField, BooleanField, PasswordField, TextAreaField, validators

The second import line imports 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

Two additional notes:

  1. The form will take values from the request for you, no need to pass in request.form.

  2. validate_on_submit() tests for the request method too, no need to do so yourself.

The following then is enough:

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

    if form.validate_on_submit():

这篇关于AttributeError:'EditForm'对象没有属性'validate_on_submit'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆