Django-刪除Mysql數(shù)據(jù)表數(shù)據(jù)操作
1. 單條數(shù)據(jù)刪除操作
1.1 views視圖操作
#刪除購物車表中的單條數(shù)據(jù)
def del_one(request, id):
#獲取該條數(shù)據(jù)的id
shopping = shopping_cart.objects.get(pk = id)
#執(zhí)行刪除操作
delete = shopping.delete()
#打印執(zhí)行結(jié)果
print(shopping,delete)
#重定向返回當前操作界面
return redirect(reverse('index:shopping'))
1.2配置當前應(yīng)用的url
url(r'^del_one/(\d+)/$', views.del_one, name = 'del_one'), # 刪除操作的url,后面是正則pk的寫法
1.3模板實例如下
{% for item in cart_list %}
<a href="/index/shpping/">
<tr class="tr-tbody">
<td class="td-check"><input type="checkbox" checked="checked"></td>
<td class="td-goods"><a href="#" target="_blank"><img src="/static/img/{{ item.productid.pic }}/"></a><a href="#" target="_blank" class="td-goods-txt">{{ item.productid.productname }}</a></td>
<td class="td-message"><p>顏色 : 花朵鳥寶貝-淺粉<br>尺碼 : 100</p></td>
<td class="td-price"><p>{{ item.sprice }}</p><p class="td-price-past">278.00</p></td>
<td class="td-num">
<div class="td-num-btn">
<a href="javascript:;" class="td-nub-left">−</a><input type="text" value="1"><a href="javascript:;" class="td-nub-right">+</a>
</div>
</td>
<td class="td-sub"><span>{{ item.sprice }}</span></td>
<td class="td-ops"><a href="/index/del_one/{{ item.id }}/">刪除</a></td>
</tr>
</a>
{% endfor %}
以上是一個購物車界面的實例
2. 全部數(shù)據(jù)刪除操作
2.1 views視圖操作
#刪除購物車中的全部數(shù)據(jù)
def del_all(request):
#獲取當前用戶
utel = request.session.get('utel')
users = Adminid.objects.get(tel=utel)
#獲取當前用戶的購物車列表
cart_list = shopping_cart.objects.filter(users=users.id)
#執(zhí)行刪除命令
delete = cart_list.delete()
print(shopping,delete)
#重定向返回當前界面
return redirect(reverse('index:shopping'))
2.2配置當前應(yīng)用的url
url(r'^del_all/$', views.del_all, name = 'del_all'), # 刪除操作的url,后面是正則pk的寫法
2.3模板實例如下
<div class="shopcar-all"><input type="checkbox" checked="checked">全選 <a href="/index/del_all/">刪除</a></div>