【问题标题】:How can I give a default value for the StringField in WTForms如何在 WTForms 中为 StringField 提供默认值
【发布时间】:2021-01-04 18:03:35
【问题描述】:

我有这门课:

class New_video_form(FlaskForm):
    title = StringField('title', validators=[DataRequired()])
    genre = StringField('genre', validators=[DataRequired()])
    link = StringField('link', validators=[DataRequired()])
    image = StringField('Image', validators=[DataRequired()])
    description = StringField('description', validators=[DataRequired()])

我在这里实现了这个:

<h1 style="color: white; text-align: center; margin-top: 20px;">Editing the movie: {{ video.title }}</h1>
<form action="" method="POST" style="display: flex; flex-direction: column; align-items: center; justify-content: center; margin-top: 50px;">
    {{ form.csrf_token }}
    <div class="form-group">
        {{ form.title(class="form-control", value="{{ video.title}}", placeholder="Title") }}
    </div>
    <div class="form-group">
        {{ form.genre(class="form-control", value="{{ video.genre }}", placeholder="Genre") }}
    </div>
    <div class="form-group">
        {{ form.link(class="form-control", value="{{ video.link }}", placeholder="Video Link") }}
    </div>
    <div class="form-group">
        {{ form.image(class="form-control", value="{{ video.image}}", placeholder="Image") }}
    </div>
    <div class="form-group">
        {{ form.description(class="form-control", value="{{ video.description }}", placeholder="Description") }}
    </div>
    <button type="submit" class="btn btn-light btn-lg">Send</button>
</form>

这个表单是从这个路由发送的:

@controller.route('/edit/<int:id>/', methods=['GET','POST'])
def edit(id):
    if current_user.is_authenticated:
        form = New_video_form()
        video = Video.query.filter_by(id=id).first()
        if request.method == "POST":
            video.title = form.title.data
            video.link = form.link.data
            video.image = form.image.data
            video.genre = form.genre.data
            video.description = form.description.data
            db.session.commit()
            #add a flash message
            return redirect(url_for('index'))
        else:
            return render_template('edit.html',form=form, video=video)
    else: 
        return redirect(url_for('controller.admin'))

在这里,我将表单和将在表单中使用的数据作为视频发送,但是当我将数据放在值字段中时,表单呈现 {{ video.example }} 而不是值。 我该如何为字符串字段从 {{ video.example }} 获取值并呈现值?

ps:h1 正在渲染正确的值。

【问题讨论】:

    标签: flask flask-wtforms wtforms


    【解决方案1】:

    如果我理解正确,如果用户来编辑视频,您想显示预先填写的表单吗?

    您可以使用this API 并在初始化时将视频对象传递给表单,如下所示:

    video = Video.query.filter_by(id=id).first()
    form = New_video_form(obj=video)
    

    如果这样做,则无需为每个字段显式传递 value="{{video.title}}" 等。

    快速编辑:刚刚注意到您在db.session.commit() 之前还缺少db.session.add(video) — 除非您将更新的对象添加到会话中,否则不会提交更改。

    【讨论】:

    • 什么意思?我的回答对你有帮助吗?
    • 我只有一个问题:我所有的输入都使用相同的代码,但一个没有得到数据,为什么会这样?
    • 哪个输入字段没有填充?
    • 类型输入。
    • 您粘贴的代码在我看来是正确的。表单中的流派字段与其他字段相同。也许您在其他地方有错字?数据库中的流派是什么类型? Video模型的定义是什么?
    猜你喜欢
    • 1970-01-01
    • 2022-01-01
    • 2012-08-19
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多