WTForms-prepopulate a textarea field with a string value? WTForms-prepopulate a textarea field with a string value? flask flask

WTForms-prepopulate a textarea field with a string value?


I tried to repeat your steps, so please find following code complete:

test.py (start code)

from wtforms import TextField, RadioField, TextAreaFieldclass Contact(Form):    email = TextField('Name : ')    subject = TextField('Subject:')    description = TextAreaField('Description:', default="please add content")app = Flask(__name__)app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'@app.route('/test')def test():    contact = Contact()    return render_template('contact/contact.html', contact=contact)if __name__ == '__main__':    app.run(debug=True)

contact/contact.html

<html>  <body>    <div class="form-group">          {{ contact.description.label(class_="control-label col-xs-3") }}          <div class="col-xs-6">              {{ contact.description(class_="form-control")}}            </div>    </div> </body></html>

Project tree is as follows:

.├── templates│   └── contact│       └── contact.html└── test.py

As you may see from the given code, you should pass instance of class Contact as extra parameter to render_template. I didn't see this in your code, so I assume you missed it. In such case you should be able to see rendered TextAreaField also, but you mentioned you see it, but you don't see default value. In this snippet everything works.

P.S. I assume, you would like that message "please add content" automatically disappear when user types something. For this purpose you should update following line in the contect.html in such manner:

{{ contact.description(class_="form-control", placeholder="please add content")}}