I am using Flask-WTF to validate a form when submitted. I am using form.validate_on_submit()
, but I get the following error:
AttributeError: 'PickASong' object has no attribute 'validate_on_submit'
Why am I getting this error, and how do I fix it?
import flask
from flask import Flask
from flask import render_template
from flask import Flask
from flask import request
from flask import render_template
from flask import redirect
from flask.ext.wtf import Form
from wtforms import *
from wtforms import StringField, BooleanField
from wtforms.validators import DataRequired
from flask.ext.wtf import Form
from wtforms import Form, TextField, BooleanField, PasswordField, TextAreaField, validators
class PickASong(Form):
song = TextField(u'Song title', validators=[DataRequired()])
class PickAnumber(Form):
songNumber = IntegerField(u'Please select song number', validators=[DataRequired()])
app = Flask(__name__)
@app.route("/",methods=["POST","GET"])
def hello():
form = PickASong(csrf_enabled=False)
if form.validate_on_submit():
user = form.song.data
print user
d=str(user)
print d
# search(d)
cmd = "python Search.py --q \"" + d +'"'
os.system(cmd)
return redirect('/2')
return render_template('searchtwo.html', form=form)
You imported from flask.ext.wtf import Form
, and then subsequently imported from wtforms import *
, which includes the base WTForms Form
. So the name Form
refers to the last object bound to it, wtforms.Form
. This is the reason you avoid import *
. A quick fix is to move from flask_wtf import Form
below from wtforms import *
. The correct fix is to import only what you need, rather than everything. If you need both forms, alias one import so the names don't conflict: from flask_wtf import Form as FlaskForm
.
You have redundantly written the following imports below the large import block at the top.
from flask.ext.wtf import Form
from wtforms import Form, TextField, BooleanField, PasswordField, TextAreaField, validators
Simply do not import Form
from wtforms
, or reverse the two lines, to effect the immediate fix.
from flask.ext.wtf import Form
from wtforms import TextField, BooleanField, PasswordField, TextAreaField, validators
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.