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

前端 未结 9 1947
暗喜
暗喜 2021-02-18 22:40

Constructing a wtforms\' TextAreaField is something like this:

content = wtf.TextAreaField(\'Content\', id=\"content-area\", validators=[validators.Required()])
         


        
9条回答
  •  暖寄归人
    2021-02-18 23:06

    You could simply use this replacement widget that is remembering default values for the rendering:

    import wtforms.widgets.core
    
    class TextArea(wtforms.widgets.core.TextArea):
        def __init__(self, **kwargs):
            self.kwargs = kwargs
    
        def __call__(self, field, **kwargs):
            for arg in self.kwargs:
                if arg not in kwargs:
                    kwargs[arg] = self.kwargs[arg]
            return super(TextArea, self).__call__(field, **kwargs)
    

    Now you can add this new widget to your Field:

    content = wtf.TextAreaField(
        'Content',
        id='content-area',
        widget=TextArea(rows=50,cols=100),
        validators=[validators.Required()])
    

    You now can render this field without any extra arguments and get a 50x100 textarea.

提交回复
热议问题