SelectField选项使用WTForms动态填充TextAreaField中的默认值 [英] SelectField option to dynamically populate default value in TextAreaField using WTForms
问题描述
我有一个从数据库表填充的SelectField.我将选择内容加载到表单中,如下所示:
I have a SelectField that is populated from a database table. I load the choices into the form as follows:
@statuses.route('/new', methods=['GET', 'POST'])
@login_required
def new_status():
form = StatusForm()
form.status_cd.choices = [(a.id, a.status_cd) for a in \
Status_Code.query.order_by('status_cd')]
if form.validate_on_submit():
status = Status(author=current_user)
form.to_model(status)
db.session.add(status)
db.session.commit()
flash('The status was added successfully.')
return redirect(url_for('.index'))
return render_template('statuses/new_status.html', form=form)
查询中引用的模型如下:
The model referenced in the query is as follows:
class Status_Code(db.Model):
__tablename__ = 'status_cd'
id = db.Column(db.Integer, primary_key=True)
status_cd = db.Column(db.String(16), nullable=False)
status_detail = db.Column(db.Text)
is_option_active = db.Boolean()
date_created = db.Column(db.DateTime, default=db.func.now())
表单类如下:
class StatusForm(Form):
datetime = DateTimeField('Date / Time')
name = StringField('Name', validators=[Required()])
status_cd = SelectField('Status Code', coerce=int)
status_detail = TextAreaField('Status Detail', default="Default text",\
validators=[Required()])
submit = SubmitField('Submit')
问题
根据SelectField中选择的选项,我希望它动态设置status_detail TextAreaField的默认文本.SelectField中的值应查询数据库并返回status_detail,它应为默认文本.
Depending on the option selected in the SelectField, I want it to dynamically set the status_detail TextAreaField's default text. The value from the SelectField should query the database and return the status_detail, which should be the default text.
例如,如果我有:
id status_cd status_detail
1 Apple The apples are red.
2 Banana The bananas are yellow.
如果用户选择苹果"选项,则默认文本应为苹果为红色".
If the user selects the "Apple" option, the default text should be "The apples are red."
任何帮助将不胜感激!我正在构建我的第一个Flask应用程序,所以我对此并不陌生.
Any assistance would be greatly appreciated! I am building my first Flask app so I'm new to this.
推荐答案
以我对flask的经验,您可以通过两种方法来实现.没有正确的方法,这完全取决于您:
In my experience with flask, you can do this a couple of ways. There is no right way, and it is all up to you:
您可以加载status_detail数据,并将其放在选择选项值的data-detail标记中:
You can load the status_detail data, and place it in a data-detail tag in your select option value:
<select name='status_cd' onchange="get_status(this);">
{% for s in status %}
<option value='{{ s.id }}' data-detail='{{ s.status_detail }}'>{{ s.status_cd }} </option>
{% endfor %}
</select>
然后,您可以使用JavaScript进行一次onchange,然后可以获取数据详细信息值并更新您的文本框(这是伪代码,并非用于复制和粘贴):
Then you can do an onchange with JavaScript, which can then get the data-detail value and update your text box (this is pseudo code, not meant for copy and paste):
<script>
function onchange(o){
var value = $(o).attr('data-detail');
//do something with the value
}
</script>
OR
如果不希望将data-detail标记放入代码中,则可以动态地从数据库中提取数据,例如:
You can do it where it pulls from the database dynamically if you don't wan to put the data-detail tag in your code, like this:
与JavaScript的onchange相同,但是随后可以调用您的路由方法的Ajax调用以返回您的值(这是伪代码,不是用于复制和粘贴):
Same onchange with JavaScript, but can then do a call to an Ajax call to your routed method to return your value (this is pseudo code, not meant for copy and paste):
<script>
function onchange(o){
var value = $(o).value();
$.ajax({
url: '/my_rout',
data: value,
success : function(data){
//handle your result here
}
})
}
</script>
我希望这至少可以为您提供正确的方向,并提供一些不同的选择.
I hope this at least gets you in the right direction with some different options to choose from.
这篇关于SelectField选项使用WTForms动态填充TextAreaField中的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!