【发布时间】: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