Python 如何在flask中呈现相同页面时显示引导模式

Python 如何在flask中呈现相同页面时显示引导模式,python,flask,bootstrap-modal,render,url-redirection,Python,Flask,Bootstrap Modal,Render,Url Redirection,我使用flask和bootstrap创建了一个web应用程序。在这个应用程序中,在输入并单击submit按钮之后,我希望在引导模式下在同一页面中显示输出或结果。但我无法正确地重定向它。如果有什么解决办法,告诉我。这将非常有帮助。提前谢谢。代码如下所示 home.html <div class="container" align="left"> <div class="panel panel-default" style="border:none;border-radius

我使用flask和bootstrap创建了一个web应用程序。在这个应用程序中,在输入并单击submit按钮之后,我希望在引导模式下在同一页面中显示输出或结果。但我无法正确地重定向它。如果有什么解决办法,告诉我。这将非常有帮助。提前谢谢。代码如下所示

home.html

 <div class="container" align="left">
  <div class="panel panel-default" style="border:none;border-radius: 10px;">
    <div class="panel-body panel1">
        <form method="POST" action="/predict">
            <div class="row">
                <div class="col-sm-6"> 
                    <div class="form-group d2">
                        <label for="NewYork"><p class="p2">New York</p></label>
                        <input type="text" class="i1" name="NewYork"><br><br>
                        <small id="NYHelp" class="form-text text-muted s1">Enter 1 if the state selected is New York else enter 0</small>
                    </div>
                    <div class="form-group d2">
                        <label for="california"><p class="p2">California</p></label>
                        <input type="text" class="i1" name="California"><br><br>
                        <small id="CaliforniaHelp" class="form-text text-muted s1">Enter 1 if the state selected is New York else enter 0</small>
                    </div>
                    <div class="form-group d2">
                        <label for="Florida"><p class="p2">Florida</p></label>
                        <input type="text" class="i1" name="Florida"><br><br>
                        <small id="FloridaHelp" class="form-text text-muted s1">Enter 1 if the state selected is New York else enter 0</small>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group d2">
                        <label for="RnD_Spend"><p class="p2">RnD_Spend</p></label>
                        <input type="text" class="i1" name="RnD_Spend"><br><br>
                        <small id="RndSpendHelp" class="form-text text-muted s1">Enter RnD Spendings in US Dollar</small>
                    </div>
                    <div class="form-group d2">
                        <label for="Admin_Spend"><p class="p2">Admin_Spend</p></label>
                        <input type="text" class="i1" name="Admin_Spend"><br><br>
                        <small id="AdminSpendHelp" class="form-text text-muted s1">Enter Adminstration Spendings in US Dollar</small>
                    </div>
                    <div class="form-group d2">
                        <label for="Market_Spend"><p class="p2">Market_Spend</p></label>
                        <input type="text" class="i1" name="Market_Spend"><br><br>
                        <small id="MarketSpendHelp" class="form-text text-muted s1">Enter Market Spendings in US Dollar</small>
                    </div>
                </div>
            </div>
            <div class="row" align="right"><input class="btn btn1" type= "submit" value="Submit" data-toggle="modal" data-target="#myModal"></div>
        </form>
    </div>
 </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
 <div class="modal-dialog modal-dialog-centered" role="document">
  <div class="modal-content">
   <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
   </div>
   <div class="modal-body">
    ...
   </div>
   <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
   </div>
  </div>
 </div>
</div>

我想说的是,在表单提交后将用户重定向到同一页面,但显示模式(这不是一种良好的用户体验)通常不是一种好的做法,我建议通过使用AJAX来实现这一点

无论如何,您可以传递一个标志,告诉HTML模板呈现程序是否需要显示模式。大概是这样的:

return render_template(
  'home.html',
  prediction=model_prediction,
  show_predictions_modal=True  // This is our flag
)
然后在模板中有条件地呈现模态:

{% if show_predictions_modal %}
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
 <div class="modal-dialog modal-dialog-centered" role="document">
  <div class="modal-content">
   <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
   </div>
   <div class="modal-body">
    ...
   </div>
   <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
   </div>
  </div>
 </div>
</div>
{% endif %}
{%if show\u predictions\u modal%}
情态标题
&时代;
...
接近
{%endif%}
{% if show_predictions_modal %}
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
 <div class="modal-dialog modal-dialog-centered" role="document">
  <div class="modal-content">
   <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
   </div>
   <div class="modal-body">
    ...
   </div>
   <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
   </div>
  </div>
 </div>
</div>
{% endif %}