【问题标题】:How to display a modal from a submit button in WTForms?如何从 WTForms 中的提交按钮显示模式?
【发布时间】:2020-10-12 20:57:26
【问题描述】:

我正在编写一个 Flask Web 应用程序,并且我有一个 html 文件,其中包含来自 WTForms 的表单视图。在文件内部,它还定义了一个 modal (myModal)。 当我按下“提交”按钮时如何调用它?

我希望它出现在我的 @route 函数中,如下所示:

@posts.route("/my-page", methods=['GET', 'POST'])
@login_required
def mypage():
    form = MyForm()
    if form.validate_on_submit():
        # Execute operations with form
        # display the 'myModal' with results from the operations
        render_modal('myModal', results=results)  # Something like this?

【问题讨论】:

    标签: python forms flask modal-dialog flask-wtforms


    【解决方案1】:

    将变量传递给底层模板以表明您希望在该模板中触发模式。

    @posts.route("/my-page", methods=['GET', 'POST'])
    @login_required
    def mypage():
        form = MyForm()
        if form.validate_on_submit():
            # Execute operations with form
            # display the 'myModal' in the underlying template with results from the operations
            return render_template('underlying_template.html', results=results, show_modal=True)  # Something like this?
    

    并且在您的 html 文件中包含以下内容(示例适用于 Bootstrap 3):

       <script>
            {% if show_modal %}
                $(document).ready(function(){
                    $("#myModal").modal('show');
                });
            {% endif %}
        </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      相关资源
      最近更新 更多