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:
The form will take values from the request for you, no need to pass in request.form
.
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():
And as of Flask-WTF version 0.13 (released 2016/09/29), the correct object to use is named FlaskForm
, to make it easier to distinguish between it and the wtforms
Form
class:
from flask_wtf import FlaskForm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With