How to render my TextArea with WTForms?

Niklas R. picture Niklas R. · Nov 2, 2011 · Viewed 28.8k times · Source

To render my textareafield with a specified number of columns and rows with WTForms, how do I set the number of columns and rows? I followed the instructions from this question but it didn't work:

How to specify rows and columns of a <textarea > tag using wtforms

I tried adding a widget but it didn't work:

class AForm(Form):
    name = TextField('Name', [validators.Length(min=4)])
    title = TextField('Title', [validators.Length(min=4)])
    text = TextAreaField('Text', widget=TextArea(row=70, cols=11))
    phonenumber = TextField('Phone number')
    phonenumberhide = BooleanField('Display phone number on site')
    price = TextField('Price')
    password = PasswordField('Password')
    email = TextField('Email', [
        validators.Length(min=6, message=_('Little short for an email address?')),
        validators.Email(message=_('That\'s not a valid email address.'))
    ])

TypeError: object.new() takes no parameters

Answer

jwogrady picture jwogrady · Apr 24, 2014

Very old question, but since the WTF-Form documentation isn't clear I'm posting my working example. OP, hope you are not still working on this. :-)

form

from flask_wtf import Form
from wtforms.fields import StringField
from wtforms.widgets import TextArea

class PostForm(Form):
    title = StringField(u'title', validators=[DataRequired()])
    body = StringField(u'Text', widget=TextArea())

template

{% extends "base.html" %}
{% block title %}Create Post{% endblock %}
{% block content %}
<H3>Create/Edit Post</H3>
<form action="" method=post>
   {{form.hidden_tag()}}
   <dl>
      <dt>Title:
      <dd>{{ form.title }}
      <dt>Post:
      <dd>{{ form.body(cols="35", rows="20") }}}
   </dl>
   <p>
      <input type=submit value="Publish">
</form>
{% endblock %}