简体   繁体   English

如何从 WTForms 中的提交按钮显示模式?

[英]How to display a modal from a submit button in WTForms?

I am writing a Flask Web Application and I have an html file with the view of a form from WTForms .我正在编写Flask Web 应用程序,并且我有一个html文件,其中包含来自WTForms的表单视图 Inside the file it is also defined a modal ( myModal ).在文件内部,它还定义了一个模态myModal )。 How to call it when I press the 'submit' button?当我按下“提交”按钮时如何调用它?

I would expect it to be inside my @route function like this:我希望它像这样在我的@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?

Pass a variable to the underling template to show you want the modal triggered in that template.将变量传递给底层模板以显示您希望在该模板中触发模态。

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

And in your html file have something like the following (example is for Bootstrap 3):并且在您的 html 文件中有如下内容(示例适用于 Bootstrap 3):

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

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

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