AttributeError: 'EditForm' object has no attribute 'validate_on_submit'

前端 未结 1 907
余生分开走
余生分开走 2021-02-15 19:10

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\' Ca

相关标签:
1条回答
  • 2021-02-15 19:23

    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():
    

    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
    
    0 讨论(0)
提交回复
热议问题