如何隐藏/显示多个wtform标签。目前我正在这样做。
HTML:
current_meeting = db.session.query(Meeting). \
filter(Meeting.meeting_date > func.now()). \
order_by(asc(Meeting.meeting_date)). \
limit(1). \
subquery('current_meeting')
meeting_obj = db.session.query(Meeting, (current_meeting.c.id > 0).label('current')). \
outerjoin(current_meeting, Meeting.id == current_meeting.c.id). \
filter(Meeting.id == meeting_id). \
order_by(Meeting.id). \
first()[0]
JQuery的:
{{ render_field(var_1, class = "class_1") }}
{{ render_field(var_2, class = "class_1") }}
{{ render_field(var_3, class = "class_1") }}
有更好的方法吗?例如,为标签分配一个类?
谢谢!
答案 0 :(得分:0)
如果您出于某些特定原因想要显示或隐藏字段,则必须执行两项操作。 首先显示您想要的字段并隐藏您不想要的字段。它可以使用jquery和css完成。
$("any id or class").click(function(){
$('#parent_div_of_field_id_that_you_want_to_show').css('display','block');
$('#parent_div_of_field_id_that_you_want_to_show').attr('data-parsley-required','false') //if you are using parsley for client side validation
$('#parent_div_of_field_id_that_you_want_to_hide').css('display','none');
$('#parent_div_of_field_id_that_you_want_to_hide').attr('data-parsley-required','true') //if you are using parsley for client side validation
现在您必须处理服务器端验证。您必须停止对这些隐藏字段的验证。为此,请对wtforms使用StopValidation。
from wtforms.validators import StopValidation
def functionName(form, field):
if form.your_field_name.data==None:
field.errors[:] = []
raise StopValidation()
这个功能就像这样
name=StringField("Name", [functionName, validators.DataRequired("Name is required")])