2

I'm trying to pepopulate a wtforms-textareafield with a value.

I defined a class:

from flask_wtf import Form from wtforms import TextField, RadioField, TextAreaField class Contact(Form): email = TextField('Name : ') subject = TextField('Subject:') description = TextAreaField('Description:', default="please add content") 

I'm using python, here i render template :

from app.mod_contact.contact import Contact //some code contact = Contact() return render_template('contact/contact.html') 

In template i did this:

<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> 

But the default text is not displayed,it's blank.

I also tried to add value on template:

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

But no result. Can somebody help me? Thanks in advance.

4
  • I checked this on my Flask instance and it works OK with default keyword. I assume your problem might be somewhere in re-defining field description. Commented Sep 29, 2015 at 11:55
  • @wanderlust i tried redefining but still the same problem, do u have any other idea? Commented Sep 29, 2015 at 12:53
  • can you put full snipet for class declaration and initialization as well? Commented Sep 29, 2015 at 14:08
  • @wanderlust i edited my question :) Commented Sep 29, 2015 at 14:26

1 Answer 1

1

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

test.py (start code)

from wtforms import TextField, RadioField, TextAreaField class 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")}} 
Sign up to request clarification or add additional context in comments.

3 Comments

I didn't copied right because parameter contact=contact in my form. I don't know why is not working still..anyway thank you for ur answer.
Try with the above code, and then extend it to your needs.
i notice an weird thing , if i do an inspect element on textareafield it has a string value, but that value is not appeared in page ?