在Flask中单击按钮时隐藏/显示表单

时间:2018-12-03 03:33:05

标签: html python-3.x python-2.7 flask backend

我正在学习创建wtf Flask Web表单,该表单是:

class Update(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    email = StringField('Email', validators=[DataRequired(), Email()])
    pic = FileField('Update Profile Pic', validators=[FileAllowed(['jpg','png'])])
    submit = SubmitField('Update')

我想做的是,单击一次表单即可将表单加载到同一页面上,而无需为表单创建单独的html页面。如何使用Flask或HTML来完成?如果必须更改路线,请也提及。

HTML代码:

<div class="content-section">
            <form method="POST" action="" enctype="multipart/form-data">
                {{ form.hidden_tag() }}
                <fieldset class="form-group">
                    <legend class="border-bottom mb-4">Account Info</legend>
                    <div class="form-group">
                        {{ form.username.label(class="form-control-label") }}

                        {% if form.username.errors %}
                        {{ form.username(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.username.errors %}
                            <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                        {% else %}
                        {{ form.username(class="form-control form-control-lg") }}
                        {% endif %}
                    </div>
                    <div class="form-group">
                        {{ form.email.label(class="form-control-label") }}
                        {% if form.email.errors %}
                        {{ form.email(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.email.errors %}
                            <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                        {% else %}
                        {{ form.email(class="form-control form-control-lg") }}
                        {% endif %}
                    </div>
                    <div class="form-group">
                        {{ form.pic.label() }}
                        {{ form.pic(class="form-control-file") }}
                        {% if form.pic.errors %}
                        {% for error in form.picture.errors %}
                        <span class="text-danger">{{error}}</span><br>
                        {% endfor %}
                        {% endif %}
                    </div>
                </fieldset>
                <div class="form-group">
                    {{ form.submit(class="btn btn-outline-info") }}
                </div>
            </form>

1 个答案:

答案 0 :(得分:1)

您可以正常加载表单,并将其可见性设置为hidden。如果有人单击该按钮,只需将表单的可见性更改为visible

以下示例:

function toggle_display(){
  el = document.querySelector('.content_section');
  
  if(el.style.visibility == 'hidden'){
      el.style.visibility = 'visible'
  }else{
     el.style.visibility = 'hidden'
  }
}
<button onclick="toggle_display()">Toggle display</button>

<div class="content_section">See me no more</div>

编辑:

还有一件值得一提的事情,visibility属性设置为hidden会占用空间,即使它是隐藏的也是如此。要完全删除表单将占用的空间并隐藏表单,请将display属性设置为none。要再次显示,请将display设置为block