简体   繁体   中英

has no attribute validate_on_submit: How to use wtforms-alchemy's ModelForm in a flask handler / view

I'm trying to switch from wtforms.ext.sqlalchemy.orm import model_form to using wtforms_alchemy ModelForm:

from wtforms_alchemy import ModelForm 

I'm failing miserably... and I've been unsuccessful searching for a working example of that uses wtforms_alchemy which shows both the handler and the model.

Using model_form works for me: Here's my working code using model forms:

The model script (python):

from app import db

class Test(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    test1 = db.Column(db.String())
    test2 = db.Column(db.String())

The handler script (python, flask, model_form):

from flask.ext.wtf import Form
from wtforms.ext.sqlalchemy.orm import model_form
from wtforms import validators

from app import app
from app import db

from app.models import *

@app.route('/', methods=['GET', 'POST'])
@login_required
def index():
    TestForm = model_form(Test, base_class = Form, db_session=db.session, field_args = {
        'test1' : {
            'validators' : [validators.Length(max=1)]
        }
    })
    form = TestForm()

    if form.validate_on_submit():
        flash("success")
        new_test = Test()
        form.populate_obj(new_test)
        db.session.add(new_test)
        db.session.commit()        
        return redirect(url_for("index"))
    return render_template("main.html", form=form)

The template (uses mako):

<form class="form" action="/" method="POST">
    % for f in form:
        %if f.widget.input_type != 'hidden':
            <dt>${ f.label }</dt>
        % endif

      <dd> 
        ${f}
        % if f.errors:
            <div class="flashes alert alert-danger">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>

                % for e in f.errors:
                    <p>${e}</p>
                % endfor
            </div>
        % endif
      </dd>
    % endfor
    <input type="submit" value="go">
 </form>

When I try to change my handler script page over to using ModelForm with this code (below) I get a 'TestForm' object has no attribute 'validate_on_submit' error

The handler script (python, flask, ModelForm):

from flask.ext.wtf import Form
from wtforms_alchemy import ModelForm
from wtforms import validators

from app import app
from app import db

from app.models import *

class TestForm(ModelForm):
    class Meta:
        model = Test

@app.route('/', methods=['GET', 'POST'])
@login_required
def index():
    form = TestForm()

    if form.validate_on_submit():
        flash("success")
        new_test = Test()
        form.populate_obj(new_test)
        db.session.add(new_test)
        db.session.commit()        
        return redirect(url_for("index"))
    return render_template("main.html", form=form)

What am I missing?

Nevermind.

Although I had tried using

from wtforms_alchemy import model_form_factory

prior to this post, it decided to work when I put it back, but after the ModelForm import instead of before it.

Whatever.

change import --> " from wtforms_sqlalchemy.orm import model_form " change import --> " from flask_wtf import FlaskForm " 将基类从 'Form' 更改为 'FlaskForm'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM