首页 > 解决方案 > 如何在模板中的 if 语句中保持模式打开

问题描述

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Order Completed</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>
            <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
        </div>
    </div>


{% if accepted_order %}
  <div class="collapse" id="collapseAcceptedCreatorOrders">
    <div class="card card-body">
      <h1>Buyer: <a href="{% url 'buyer_page'%}">{{ accepted_order }}</a></h1>
        <a href="{% url 'dashboard' %}">
          <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
            Mark As Bought
          </button>
        </a>
        <a href="{% url 'dashboard_withdraw_order' %}"><button>Withdraw</button></a>
    </div>
  </div>
{% endif %}

我试图在用户单击此if语句中的按钮时显示模式。唯一的问题是单击按钮时,模式会弹出一瞬间然后消失。当我在语句之外使用按钮时,if它可以正常工作,但我需要在if语句中使用它。我怎样才能做到这一点?

标签: django

解决方案


经过一些试验和错误,我想通了。当模板中的 {% url ... %} 命令运行时,显然会刷新页面,这是我不知道的。所以,我所做的就是在模态中添加该部分。基本上,不是将打开模式的按钮(即“标记为已购买”)封装在运行逻辑的链接中。我只是在 if 语句中放了一个按钮,然后打开模态,模态内部是运行逻辑的地方。


推荐阅读