后端:
一、跟前文如何查询数据库中的数据:https://blog.csdn.net/ahc176/article/details/119080900?spm=1001.2014.3001.5501
二、修改customer.py的内容:
import json from django.http import JsonResponse from czx.models import Customer def dispatcher(request): # 将请求参数统一放入request的params属性中 # GET请求参数在request对象的GET属性中 if request.method == 'GET': request.params = request.GET # POST/PUT.DELETE请求参数从request对象的body属性中获取 elif request.method in ['POST', 'PUT', 'DELETE']: # 根据接口,请求的消息体都是json格式 request.params = json.loads(request.body) # 根据不同的action分派给不同你的函数进行处理 action = request.params['action'] if action == 'list_customer': return listcustomers(request) elif action == 'add_customer': return addcustomer(request) elif action == 'delete_customer': return deletecustomer(request) else: return JsonResponse({'ret': 1, 'msg': '不支持该类型http请求'}) def listcustomers(request): qs = Customer.objects.values() retlist = list(qs) return JsonResponse({'ret': 0, 'retlist': retlist}) def addcustomer(request): info = request.params['data'] #从请求信息获取要添加的客户信息 #插入数据库 record = Customer.objects.create( name=info['name'], phonenumber=info['phonenumber'], address=info['address'] ) return JsonResponse({'ret':0,'id':record.id}) def deletecustomer(request): customerid=request.params['id'] try: customer = Customer.objects.get(id=customerid) except Customer.DoesNotExist: return { 'ret':1, 'msg':f'id为’{customer}‘的客户不存在' } customer.delete() return JsonResponse({'ret':0})
后端完成。
前端:
一、建一个删除页
二、delete.js
// index.js // 获取应用实例 const app = getApp() Page({ data:{ id:null, res:1 }, bindinput:function(e){ this.setData({ id: e.detail.value }) }, delete: function () { var that = this; wx.request({ url: 'http://127.0.0.1:8000/api/mgr/customers', method:'DELETE', data:{ action:'delete_customer', id:that.data.id }, success(data){ console.log(data.data.ret) that.setData({res:data.data.ret}) } }) } })
三、delete.wxml
<view class="view-contain-ti"> <text class="text-ti">用户id</text> <input class="input1" bindinput="bindinput"></input> </view> <button type="primary" size="{{primarySize}}" loading="{{loading}}" plain="{{plain}}" disabled="{{disabled}}" bindtap="delete"> 删除用户 </button> <view wx:if="{{res==0}}">删除成功</view>
四、delete.wxss
.view-contain-ti { display: flex; height: 40px; margin: 20px; border: 3rpx solid #2b8847; border-radius: 10rpx; } .text-ti { position: absolute; font-size: 12px; background: white; margin: -10px 0 0 10px; padding: 0 5px; color: rgb(144, 147, 167); } .input1 { margin: auto 10px; }
前端完成。
效果:
输入要删除的id号:
点击删除:
删除成功