无法在flas上显示引导模式

2024-04-18 21:30:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我用flask和bootstrap创建了一个web应用程序。在这个应用程序中,在输入并单击submit按钮之后,我想在bootstrap模式的同一个页面中显示输出或结果。但我无法正确地重定向它。你知道吗


应用程序类型

@app.route("/action_distance", methods=['POST', 'GET'])
def action_distance():
    message = "Distance"
    if request.method == 'POST':
        key_1 = request.values.get("key_1")
        print("key_1 = ", key_1)
        key_2 = request.values.get("key_2")
        print("key_2 = ", key_2)
        x1, y1, z1 = get_coordinates(key_1)
        x2, y2, z2 = get_coordinates(key_2)
        object_one = Coordinates(x1, y1, z1)
        object_two = Coordinates(x2, y2, z2)
        distance = distance_math(object_one, object_two)
        distance = round(float(distance), 2)
        print("distance = ", distance)
        return render_template("distance.html",
                               msg=message,
                               distances=distance,
                               show_modal=True)

我直接把distances传递给html文件。 距离.html

    <form method="POST" action="/action_distance">
                <label class="my-1 mr-2" for="inlineFormCustomSelectPref">Preference</label>
                <select name="key_1" class="custom-select my-1 mr-sm-2" id="inlineFormCustomSelectPref">
                    <option selected>Choose...</option>
                    {% for row in rows_1 %}
                        <option Value="{{row}}" name="key_1">{{row}}</option>
                    {% endfor %}
                </select>
                <select name="key_2" class="custom-select my-1 mr-sm-2" id="inlineFormCustomSelectPref">
                    <option selected>Choose...</option>
                    {% for row in rows_2 %}
                        <option Value="{{row}}" >{{row}}</option>
                    {% endfor %}
                </select>
            <button type="submit" class="btn btn-primary my-1">Submit</button>
        </form>
    {% if show_modal == True %}
    <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog" role="document">
    <div class="modal-content">
    <div class="modal-header">
    <h5 class="modal-title">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">
       <p>Modal body text goes here.</p>
    </div>
    <div class="modal-footer">
       <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
       <button type="button" class="btn btn-primary">Save changes</button>
     </div>
   </div>
  </div>
</div>
{% endif %}

但是,单击按钮后,模式引导不会出现。但当我尝试以下方法时:

{% if show_modal ==True %}
        <p>distance is : {{ distances }}</p>
{% endif %}

它确实显示传递的值。你知道吗


Tags: keydivgetobjectmytypebuttonaction