django - 如何在模板中的 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">×</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
语句中使用它。我怎样才能做到这一点?
解决方案
经过一些试验和错误,我想通了。当模板中的 {% url ... %} 命令运行时,显然会刷新页面,这是我不知道的。所以,我所做的就是在模态中添加该部分。基本上,不是将打开模式的按钮(即“标记为已购买”)封装在运行逻辑的链接中。我只是在 if 语句中放了一个按钮,然后打开模态,模态内部是运行逻辑的地方。
推荐阅读
- javascript - 防止按钮在纯 JavaScript 的浏览器 URL 中显示井号和 id 名称
- sql - UNION vs. JOIN,b 和 c 没有定义?(sqlite3)
- unit-testing - 模拟接口函数没有被调用
- java - Java抽象类获取扩展类的类型
- angular - 在 Angular5 中使用服务不起作用
- javascript - 如何在 GraphQL 中返回一组对象,可能使用与返回单个对象的端点相同的端点?
- r - 为另一行的每个值计算某个列的值而不是 NA
- node.js - Alexa 中的 CanfulFillIntentRequest 没有响应
- laravel - 带有反射参数的 Laravel 类方法
- python-3.x - 如何在python中从一个函数绑定到另一个函数