简体   繁体   English

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

[英]How to show flash messages in bootstrap modal window?

Here is the problem.这是问题所在。 I have a bootstrap modal window with forms where i show up error messages with flask.flash() if any happend.我有一个带有表单的引导模式窗口,如果发生任何情况,我会在其中显示带有 flask.flash() 的错误消息。 But!但! When I click upload for the first time my modal window is closing, and I can see the error message only when I reopen the modal window.当我第一次单击上传时,我的模态窗口正在关闭,只有当我重新打开模态窗口时才能看到错误消息。 So the error message should show up when I click Upload button without closing the modal window, if the error happend, but if there is no errors, it should work as it is.因此,当我在不关闭模式窗口的情况下单击“ Upload按钮时,应该会显示错误消息,如果发生错误,但如果没有错误,则应按原样工作。 How to make it behave the way I want?如何让它按照我想要的方式行事? Is it js stuff?是js的东西吗? (which I don't know) Or it could be made by flask and bootstrap? (我不知道)或者它可以由烧瓶和引导程序制成? Anyway, need your help guys.无论如何,需要你们的帮助。

上传表格 上传带有错误消息的表单

<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>

Yeah, I was stuck with this too.是的,我也被这个困住了。 There is a easy solution with JQuery: This code basically opens up the modal only if the error (flash warning exist) JQuery 有一个简单的解决方案:此代码基本上仅在出现错误时才打开模态(存在闪光警告)

where .flash is the div that you named for your flash warning and其中 .flash 是您为 Flash 警告命名的 div 和

changePasswordModal is the modal ID 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM