【问题标题】:Populate textarea field using WTForms使用 WTForms 填充 textarea 字段
【发布时间】:2020-09-17 15:17:31
【问题描述】:

据我了解,如果要填充 textarea,请将文本放在 textarea 标签之间。但是我正在使用 WTForms。如何从视图或模板中预填充表单?

表格

class ModuleSectionForm(FlaskForm):
    title = StringField('Section Title', validators=[DataRequired()])
    description = TextAreaField('Description', validators=[DataRequired()])
    submit = SubmitField('Add Section')

查看

@modules.route('/update_section/<name>/<title>', methods=['GET', 'POST'])
def update_section(name, title):
    form = ModuleSectionForm()
    module = Module.objects(title=name).first()
    section = None
    for sect in module.sections:
        if sect.title == title:
            section = sect
    #if form.validate_on_submit():
        #save data 
    return render_template('modules/update_section.html', section=section, form=form) 

模板

<form method="post" action="{{ url_for('modules.update_section', name=name) }}">
    {{ form.hidden_tag() }}
    <div class="form-group">
        {{ form.title.label(class="form-control-label") }}
        {{ form.title(class="form-control", value=section.title) }}
    </div>
    <div class="form-group">
        {{ form.description.label(class="form-control-label") }}
        {{ form.description(class="form-control", default=section.description) }}
    </div>
    <div class="form-group">
        {{ form.submit(class="btn btn-secondary shadow") }}
    </div>
</form>

【问题讨论】:

  • 你有没有想过这个问题?

标签: python forms flask textarea wtforms


【解决方案1】:

这可以通过预先指定要显示的文本来完成(例如在视图中)。

尝试以这种方式编辑您的视图 (update_section):

+ form.description.data = 'text you want to display'

你的模板如下:

- {{ form.description(class="form-control", default=section.description) }}
+ {{ form.description(class="form-control") }}

请注意,还有另一种方法,即指定占位符属性(但我想这不是你想要做的)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多