顧客一覧ページから、対象の削除ボタンを押下するとremodalで確認ボタンが表示され、OKを押すとレコードが削除されるようにする。
### mysql
mysql> select * from sales_clients;
-> 2つレコードが入っています。
### view
– remodalをformにして、client_idをinput hiddenで削除完了ページにpostします。
remodal
{% block script %}
<div class="remodal" data-remodal-id="modal">
<form method="post" action="/client/delete" id="form_id">
{% csrf_token %}
<button data-remodal-action="close" class="remodal-close"></button>
<input type="hidden" id="client_id" name="client_id">
<h1>得意先削除</h1>
<p>
<span id="company_name"></span>を削除して宜しいでしょうか?
</p>
<br>
<button data-remodal-action="cancel" class="remodal-cancel">Cancel</button>
<button data-remodal-action="confirm" class="remodal-confirm">OK</button>
</form>
</div>
<script src="{% static 'sales/js/remodal.min.js' %}"></script>
<script>
var remodal = $('[data-remodal-id=modal]').remodal();
$('.del').on('click', function(){
var id = $(this).attr("id");
var name = $(this).attr("value");
document.getElementById("company_name").innerHTML = name;
document.getElementById("client_id").setAttribute("value", id);
})
$(document).on('confirmation', remodal, function(){
$('#form_id').submit();
});
</script>
{% endblock %}


### views.py
views.pyでは、*.objects.getで対象のレコードを取得して削除します。
/sales/views.py
def client_delete(request): if(request.method == 'POST'): client_id = request.POST['client_id'] client = Clients.objects.get(id=client_id) client.delete() return render(request, 'sales/client_delete.html') return render(request, 'sales/client_delete.html')
### 削除実行
mysql> select * from sales_clients;
+—-+——————————————–+——————————–+——–+————+—————–+————–+————————-+———-+————+——————————-+————–+————–+————–+—————–+——–+—————————-+—————————-+
| id | name | name_kana | office | department | position | charge | charge_mail | zipcode | prefecture | address | tel | fax | name_top | position_top | remark | created_at | updated_at |
+—-+——————————————–+——————————–+——–+————+—————–+————–+————————-+———-+————+——————————-+————–+————–+————–+—————–+——–+—————————-+—————————-+
| 1 | ジャパンソフトウェア株式会社 | ジャパンソフトウェア | 本社 | 営業部 | 代表取締役 | 佐藤太郎 | staro@japansoftware.com | 100-0002 | 東京都 | 千代田区皇居外苑1-2-3 | 03-1234-5678 | 03-1234-5679 | 山本五郎 | 代表取締役 | | 2020-08-30 02:09:35.555187 | 2020-08-30 02:09:35.555259 |
+—-+——————————————–+——————————–+——–+————+—————–+————–+————————-+———-+————+——————————-+————–+————–+————–+—————–+——–+—————————-+—————————-+
1 row in set (0.00 sec)

deleteはviews.pyで*.delete()で削除できる。
DjangoのCRUDは一通りマスターしました。
ところでDjangoは論理削除の機能はあるんでしょうか? is_activeのカラムを作って、削除時にupdateするのかな。