.py入mongo:

class Collect(View): def __init__(self): self.mongo = Mongodb()   def post(self,request): username = request.POST.get('username') id = request.POST.get('id') res = self.mongo.find_one({'username':username})
if res:
my_list = res['id'] my_list.append(int(id)) self.mongo.update_one({'username':username},{'$set':{'id':my_list}})
else: db_list = [int(id)] self.mongo.insert({'username':username,'id':db_list}) # return HttpResponse(res) return HttpResponse('收藏成功')  

.py入redis:

class Collect(View): 定义添加方法 def post(self,request):
#接收参数 id = request.POST.get('id','未接到参数') username = request.POST.get('username','未接到参数') print(f'这里是用户名的id:{username}')
cart_list = cache.get(username,'0')
#如果不存在就创建后添加 if cart_list == '0': clist = [] clist.append(int(id)) cache.set(username,clist)
else: cart_list.append(int(id)) cache.set(username,cart_list,360000) cart_list=cart_list # print(cart_list)
return HttpResponse('添加成功')    

.html

  //加入收藏逻辑             function colle(id){                 username = $.cookie('username');                 alert(username);                 //获取url中的参数                 function getUrlParam(id) {                 var reg = new RegExp("(^|&)" + id + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象                 var r = window.location.search.substr(1).match(reg); //匹配目标参数                 if (r != null) return unescape(r[2]); return null; //返回参数值                 }                 var id = getUrlParam('id');
                //删除页面袁术                          //请求接口删除库里面的数据             let param = new URLSearchParams();             //将参数传递给对象             param.append('id',id);             param.append('username',username);             // param.append('name',name);             // param.append('price',price);                                           axios({                         //指定请求地址                         url:'http://localhost:8000/md_admin_user/colles',                         //请求参数                         data:param,                         //请求类型                         method:'post',                         //接口返回值类型                         responseType:'text',
                })
                .then(function(obj){                                          console.log(obj);                             // alert(obj.data);                                 if(obj.data == '收藏成功'){                                     // window.location.href = 'http://localhost:8080/user_center_collect.html'                                                                      }

                                         });      }
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄