如何在引导模式窗口中显示Flash消息?

时间:2019-01-17 06:55:23

标签: javascript flask bootstrap-4

这是问题所在。我有一个窗体的引导模式窗口,如果发生任何错误,我会在其中显示flask.flash()的错误消息。但!当我第一次单击上载时,我的模式窗口正在关闭,只有重新打开模式窗口时,我才能看到错误消息。因此,当我单击Upload按钮而不关闭模式窗口时,错误消息应该显示,如果发生错误,但是如果没有错误,则应该照原样工作。如何使其表现出我想要的方式?是js的东西吗? (我不知道)还是可以用烧瓶和靴子制成?无论如何,需要您的帮助。

Upload form Upload form with error message

<form method="post" action="{{ url_for('index') }}" enctype="multipart/form-data">
                <div class="modal-body">
                    {% with messages = get_flashed_messages(with_categories=true) %}
                        {% if messages %}
                            {% for category, message in messages %}
                            <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                               {{ message }}
                              <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                              </button>
                            </div>
                            {% endfor %}
                        {% endif %}
                    {% endwith %}

                    <div class="input-group mb-3">
                      <div class="custom-file">
                        <input type=file name=dump_file class="file-input" id="custom-file-input" multiple>
                        <label class="custom-file-label" for="custom-file-input" data-browse="Browse">Choose file</label>
                      </div>
                    </div>
                    <div class="custom-control custom-radio custom-control-inline">
                      <input type="radio" id="customRadioInline1" name="radio" class="custom-control-input" value="linux">
                      <label class="custom-control-label" for="customRadioInline1">Linux</label>
                    </div>
                    <div class="custom-control custom-radio custom-control-inline">
                      <input type="radio" id="customRadioInline2" name="radio" class="custom-control-input" value="windows">
                      <label class="custom-control-label" for="customRadioInline2">Windows</label>
                    </div>
                    <!-- <span class="glyphicon glyphicon-paperclip"></span> -->
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-info">Upload</button>
                </div>
          </form>
      </div>
    </div>
  </div>

1 个答案:

答案 0 :(得分:1)

是的,我也对此感到困惑。 JQuery有一个简单的解决方案: 基本上,只有在错误(存在闪光警告)的情况下,这段代码才会打开模态

其中.flash是您为Flash警告命名的div,

changePasswordModal是模式ID

<script>
    $(document).ready(function() { // When page finished loading
  if ( $('.flash').length ) { // if there is an DOM that has class has-error
     $('#changePasswordModal').modal('show'); // Show Modal
  }
});
</script>