I'm trying to use model_form
in WTForms to automatically generate the Form
for a given model; however, when I use it, I get the following error:
UndefinedError: 'wtforms.ext.sqlalchemy.orm.UserForm object'
has no attribute 'hidden_tag'
Here's what my code looks like:
from flask import Flask, render_template
from flask.ext.wtf import Form
from flask.ext.sqlalchemy import SQLAlchemy
from wtforms.ext.sqlalchemy.orm import model_form
app = Flask(__name__)
app.config.from_pyfile('app.cfg')
db = SQLAlchemy(app)
class User(db.Model):
"""Database model for SQLAlchemy."""
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
UserForm = model_form(User, Form)
@app.route('/')
def index():
"""Show subscription form."""
form = UserForm()
return render_template('form.html', form=form)
And in the template:
<form class="register" action="{{ url_for('form_subscribe') }}" method="POST" >
{{ form.hidden_tag() }}
I've checked several places including other questions but I can't find the appropriate best practice solution for this.
Apparently the arguments order for the model_form
changed between versions [1] [2], so it should be called as:
UserForm = model_form(User, base_class=Form)
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