def post(self, request): fav_id = request.POST.get('fav_id', '0') fav_type = request.POST.get('fav_type', '0') if not request.user.is_authenticated: """ 判断用户登陆状态 """ return HttpResponse('{"status":"fail", "msg":"用户未登陆"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_records: # 如果记录已经存在,则表示用户取消收藏 exist_records.delete() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_id = int(fav_type) user_fav.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = int(request.POST.get('fav_id', 0)) fav_type = int(request.POST.get('fav_type', 0)) res = dict() if not request.user.is_authenticated(): res['status'] = 'fail' res['msg'] = '用户未登录' return HttpResponse(json.dumps(res), content_type='application/json') # 查询收藏记录 exist_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if exist_records: exist_records.delete() self.set_fav_nums(fav_type, fav_id, -1) res['status'] = 'success' res['msg'] = '收藏' else: user_fav = UserFavorite() if fav_id and fav_type: user_fav.user = request.user user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.save() self.set_fav_nums(fav_type, fav_id, 1) res['status'] = 'success' res['msg'] = '已收藏' else: res['status'] = 'fail' res['msg'] = '收藏出错' return HttpResponse(json.dumps(res), content_type='application/json')
def post(self, request): # 表明你收藏的不管是课程,讲师,还是机构。他们的id # 默认值取0是因为空串转int报错 id = request.POST.get('fav_id', 0) # 取到你收藏的类别,从前台提交的ajax请求中取 type = request.POST.get('fav_type', 0) # 收藏与已收藏取消收藏 # 判断用户是否登录:即使没登录会有一个匿名的user if not request.user.is_authenticated: # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(id), fav_type=int(type)) if exist_records: # 如果记录已经存在, 则表示用户取消收藏 exist_records.delete() if int(type) == 1: course = Course.objects.get(id=int(id)) course.fav_nums -= 1 if course.fav_nums < 0: course.fav_nums = 0 course.save() elif int(type) == 2: org = CourseOrg.objects.get(id=int(id)) org.fav_nums -= 1 if org.fav_nums < 0: org.fav_nums = 0 org.save() elif int(type) == 3: teacher = Teacher.objects.get(id=int(id)) teacher.fav_nums -= 1 if teacher.fav_nums < 0: teacher.fav_nums = 0 teacher.save() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() # 过滤掉未取到fav_id type的默认情况 if int(type) > 0 and int(id) > 0: user_fav.fav_id = int(id) user_fav.fav_type = int(type) user_fav.user = request.user user_fav.save() if int(type) == 1: course = Course.objects.get(id=int(id)) course.fav_nums += 1 course.save() elif int(type) == 2: org = CourseOrg.objects.get(id=int(id)) org.fav_nums += 1 org.save() elif int(type) == 3: teacher = Teacher.objects.get(id=int(id)) teacher.fav_nums += 1 teacher.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): # 获得从前端传来的fav_id, fav_type fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) # 判断用户是否登陆,request有一个匿名类user,可用于判断用户是否登陆 if not request.user.is_authenticated: # 如果返回fail,ajax中自动跳转等登录页面,里面的msg值要和ajax中的值相同 return HttpResponse('{"status": "fail", "msg":"用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) # 如果记录已存在,就取消收藏 if exist_records: exist_records.delete() return HttpResponse('{"status": "fail", "msg":收藏"}', content_type='application/json') # 记录不存在,就收藏 else: user_fav = UserFavorite() # 过滤掉没取到fav_id, fav_type的默认情况 if int(fav_type) > 0 and int(fav_id) > 0: user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.user = request.user user_fav.save() return HttpResponse('{"status": "success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status": "fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): result = {"code": 10000, "message": "", "status": ""} id = request.session.get("id") fav_id = request.POST.get("fav_id") fav_type = request.POST.get("fav_type") if fav_id and fav_type: if id: fav_obj = UserFavorite.objects.filter(user_id=int(id), fav_type=fav_type, fav_id=fav_id).first() if fav_obj: fav_obj.delete() result["message"] = "取消收藏成功" result["status"] = "0" result["code"] = 10003 else: fav_obj = UserFavorite() fav_obj.fav_id = int(fav_id) fav_obj.fav_type = fav_type fav_obj.user_id = int(id) fav_obj.save() result["message"] = "收藏成功" result["code"] = 10000 result["status"] = "1" else: result["message"] = "请您先进行登录才能收藏" result["code"] = 10002 result["status"] = "2" else: result["message"] = "无法获取到数据" result["code"] = 10004 result["status"] = "3" return JsonResponse(result)
def post(self,request): fav_id = request.POST.get('fav_id', 0) #获取收藏id,默认设置为0 fav_type = request.POST.get('fav_type', 0) #获取收藏类型,默认设置为0 if request.user.is_authenticated: #判断用户是否登录,如果未登录,is_authenticated为True,表示用户未登录 #判断用户登录状态 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') # 失败,返回失败原因,content_type='application/json'为固定的写法 #后台控制不了ajax跳转,跳转在ajax中进行 exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type = int(fav_type)) if exist_records: # 如果记录已经存在,则表示用户取消收藏 exist_records.delete() #存在则删掉 return HttpResponse('{"status":"fail", "msg":"收藏"}', content_type='application/json') else:#如果不存在,则在数据库里添加记录 user_fav = UserFavorite() #实例化 if int(fav_id) >0 and int(fav_type)>0: #如果都大于0,则添加 user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() #保存,添加到数据库 return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') # 失败,返回失败原因,content_type='application/json'为固定的写法 #后台控制不了ajax跳转,跳转在ajax中进行 else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json') # 失败,返回失败原因,content_type='application/json'为固定的写法 #后台控制不了ajax跳转,跳转在ajax中进行
def post(self, request): # 表明你收藏的不管是课程,讲师,还是机构。他们的id # 默认值取0是因为空串转int报错 id = request.POST.get('fav_id', 0) # 取到你收藏的类别,从前台提交的ajax请求中取 type = request.POST.get('fav_type', 0) # 收藏与已收藏取消收藏 # 判断用户是否登录:即使没登录会有一个匿名的user if not request.user.is_authenticated: # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(id), fav_type=int(type)) if exist_records: # 如果记录已经存在, 则表示用户取消收藏 exist_records.delete() if int(type) == 1: course = Course.objects.get(id=int(id)) course.fav_nums -=1 if course.fav_nums < 0: course.fav_nums = 0 course.save() elif int(type) == 2: org = CourseOrg.objects.get(id=int(id)) org.fav_nums -= 1 if org.fav_nums < 0: org.fav_nums = 0 org.save() elif int(type) == 3: teacher = Teacher.objects.get(id=int(id)) teacher.fav_nums -=1 if teacher.fav_nums < 0: teacher.fav_nums = 0 teacher.save() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() # 过滤掉未取到fav_id type的默认情况 if int(type) >0 and int(id) >0: user_fav.fav_id = int(id) user_fav.fav_type = int(type) user_fav.user = request.user user_fav.save() if int(type) == 1: course = Course.objects.get(id=int(id)) course.fav_nums += 1 course.save() elif int(type) == 2: org = CourseOrg.objects.get(id=int(id)) org.fav_nums += 1 org.save() elif int(type) == 3: teacher = Teacher.objects.get(id=int(id)) teacher.fav_nums += 1 teacher.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = request.POST.get("fav_id", 0) fav_type = request.POST.get("fav_type", 0) if not request.user.is_authenticated(): return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exit_record = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exit_record: # 记录已经存在,则删除记录 exit_record.delete() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json') else: user_favorite = UserFavorite() if int(fav_id) != 0 and int(fav_type != 0): user_favorite.user = request.user user_favorite.fav_id = int(fav_id) user_favorite.fav_type = int(fav_type) user_favorite.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = int(request.POST.get('fav_id', 0)) fav_type = int(request.POST.get('fav_type', 0)) print(fav_id) print(fav_type) res = {} if not request.user.is_authenticated(): res['status'] = False res['msg'] = '用户未登录' return HttpResponse(json.dumps(res)) # 查询收藏记录 exist_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if exist_records: exist_records.delete() res['status'] = True res['msg'] = '收藏' else: user_fav = UserFavorite() if fav_id and fav_type: user_fav.user = request.user user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.save() res['status'] = True res['msg'] = '已收藏' else: res['status'] = False res['msg'] = '收藏出错' return HttpResponse(json.dumps(res))
def add_fan(request): if request.method == 'POST': fav_id = request.POST.get("fav_id", 0) fav_type = request.POST.get("fav_type", 0) if not request.user.is_authenticated(): return HttpResponse('{"status":"fail","msg":"用户未登录"}', content_type='application/json') exist_record = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_record: exist_record.delete() return HttpResponse('{"status":"success","msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() return HttpResponse('{"status":"success","msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = request.POST.get('fav_id', '0') fav_type = request.POST.get('fav_type', '') #判断用户是否登陆 if not request.user.is_authenticated(): return HttpResponse('{"status":"fail","msg":u"用户未登陆"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_records: #已经收藏,取消 exist_records.delete() return HttpResponse('{"status":"fail","msg":u"收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() return HttpResponse('{"status":"fail","msg":u"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail","msg":u"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = request.POST.get('fav_id', '0') fav_type = request.POST.get('fav_type', '') if not request.user.is_authenticated: info = {'status': 'fail', 'msg': 'NO_LOGIN'} return JsonResponse(info, safe=False) exist_record = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if exist_record: exist_record.delete() info = {'status': 'fail', 'msg': '♥ 收藏'} return JsonResponse(info, safe=False) else: user_fav = UserFavorite() if (fav_id > 0) and fav_type: user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.user = request.user user_fav.save() info = {'status': 'success', 'msg': '已收藏'} return JsonResponse(info, safe=False) else: info = {'status': 'fail', 'msg': '收藏失败'} return JsonResponse(info, safe=False)
def post(self, request): # # has_fav = False fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) # 判断是否登录, return后面就不执行了,在这里结束了 # 未登录状态下依然会有user类在里面 if not request.user.is_authenticated(): return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type="application/json") # 用户登录后逻辑 exist_records = UserFavorite.objects.filter(user=request.user, fav_type=int(fav_type), fav_id=int(fav_id)) if exist_records: # 记录已经存在, 则表示用户取消收藏 exist_records.delete() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type="application/json") else: # 如果不存在 user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() has_fav = True # 根据传回的值,判断是未收藏还是已收藏 return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type="application/json") else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type="application/json")
def post(self,request): fav_id = request.POST.get('fav_id',0) fav_type = request.POST.get('fav_type',0) # 判断用户登陆状态 if not request.user.is_authenticated(): return HttpResponse('{"status":"fail","msg":"用户未登录"}', content_type='application/json') else: exist_records = UserFavorite.objects.filter(user=request.user,fav_id=int(fav_id),fav_type=int(fav_type)) #用户收藏记录已经存在(已登陆),则表示用户取消收藏 if exist_records: exist_records.delete() return HttpResponse('{"status":"fail","msg":"取消收藏"}', content_type='application/json') else: if int(fav_type)>0 and int(fav_id>0): user_fav = UserFavorite() user_fav.user = request.user user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.save() return HttpResponse('{"status":"success","msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"收藏出错"}', content_type='application/json')
def post(self, request): # 默认值取0是因为空字符串转int报错 id = request.POST.get('fav_id', 0) # 取到妳收藏的类型,从前台提交的ajax请求中取 type = request.POST.get('fav_type', 0) # 判断用户是否登录,即使未登录,request也会有一个匿名user if not request.user.is_authenticated: return HttpResponse('{"status": "fail", "msg": "用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(id), fav_type=int(type)) if exist_records: # 如果记录存在,则表示用户取消收藏 exist_records.delete() if int(type) == 1: params = Course.objects.get(id=int(id)) params.fav_nums -= 1 if params.fav_nums < 0: params.fav_nums = 0 params.save() elif int(type) == 2: params = CourseOrg.objects.get(id=int(id)) params.fav_num -= 1 if params.fav_nums < 0: params.fav_nums = 0 params.save() elif int(type) == 3: params = Teacher.objects.get(id=int(id)) params.fav_nums -= 1 if params.fav_nums < 0: params.fav_nums = 0 params.save() return HttpResponse('{"status": "success", "msg": "收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(type) > 0 and int(id) > 0: user_fav.fav_id = int(id) user_fav.fav_type = int(type) user_fav.user = request.user user_fav.save() if int(type) == 1: params = Course.objects.get(id=int(id)) params.fav_nums += 1 if params.fav_nums < 0: params.fav_nums = 0 params.save() elif int(type) == 2: params = CourseOrg.objects.get(id=int(id)) params.fav_num += 1 if params.fav_nums < 0: params.fav_nums = 0 params.save() elif int(type) == 3: params = Teacher.objects.get(id=int(id)) params.fav_nums += 1 if params.fav_nums < 0: params.fav_nums = 0 params.save() return HttpResponse('{"status": "success", "msg": "已收藏"}', content_type='application/json') else: return HttpResponse('{"status": "fail", "msg": "收藏失败"}', content_type='application/json')
def post(self, request): user = request.user fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) if not user.is_authenticated(): return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') else: exist_user_fav = UserFavorite.objects.filter( user=user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_user_fav: #如果存在记录,那么代表用户取消掉收藏 exist_user_fav.delete() # return JsonResponse('{"status":"fail", "msg":"收藏"}') return HttpResponse('{"status":"fail", "msg":"收藏"}', content_type='application/json') else: #否则代表用户添加收藏 user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() # return JsonResponse('{"status":"fail", "msg":"已收藏"}') return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): #在ajax那获取两个值 fav_id = request.POST.get('fav_id', '0') fav_type = request.POST.get('fav_type', '') #判断用户是否登录 if not request.user.is_authenticated: info = {'status':'fail', 'msg':'未登录'} return JsonResponse(info, safe=False) #未登录,ajax控制页面跳转 exist_record = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) #查找是否存在收藏记录 #如果存在 if exist_record: exist_record.delete() info = {'status':'fail', 'msg':'♥ 收藏'} return JsonResponse(info, safe=False) #如果不存在 else: user_fav = UserFavorite() #判断参数正常,这里可以int(fav_id)确保数据类型 if (fav_id > 0) and fav_type: user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.user = request.user user_fav.save() info = {'status':'success', 'msg':'已收藏'} return JsonResponse(info, safe=False) else: info = {'status':'fail', 'msg':'收藏失败'} return JsonResponse(info, safe=False)
def post(self, request): id = request.POST.get('fav_id', 0) # 防止后边int(fav_id)时出错 type = request.POST.get('fav_type', 0) # 防止int(fav_type)出错 if not request.user.is_authenticated: # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_record = UserFavorite.objects.filter(user=request.user, fav_id=int(id), fav_type=int(type)) if exist_record: # 如果记录已经存在,表示用户取消收藏 exist_record.delete() # return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json') return JsonResponse({"status":"success", "msg":"收藏"}) else: user_fav = UserFavorite() if int(id) > 0 and int(type) > 0: user_fav.user = request.user user_fav.fav_id = int(id) user_fav.fav_type = int(type) user_fav.save() # return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') return JsonResponse({"status":"success", "msg":"已收藏"}) else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', '') # 是否收藏 is_fav = False if not request.user.is_authenticated: return JsonResponse({'status': 'no', 'msg': '用户未登录'}) # 查看用户是否收藏 exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=fav_type) if exist_records: # 如果有收藏该请求代表删除收藏 exist_records.delete() return JsonResponse({'status': 'yes', 'msg': '收藏'}) else: user_fav = UserFavorite() if int(fav_id) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = fav_type user_fav.save() return JsonResponse({'status': 'yes', 'msg': '已收藏'}) else: return JsonResponse({'status': 'no', 'msg': '收藏出错'})
def post(self, request): # 表明你收藏的不管是课程,还是机构,他们的id # 默认值取0是因为空串转int报错 id = request.POST.get('fav_id', 0) # 取到你收藏的类别,从前台提交的ajax请求中取 type = request.POST.get('fav_type', 0) # 收藏与已收藏取消收藏 # 判断用户是否登录:即使没登陆会有一个匿名的user if not request.user.is_authenticated(): # 未登录是返回json提示未登录,跳转到登录页面实在ajax中做的 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(id), fav_type=int(type)) if exist_records: # 如果已经存在,则 表示用户取消收藏 exist_records.delete() return HttpResponse('{"status":"success","msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() # 过滤掉未取到fav_id type默认情况 if int(type) > 0 and int(id) > 0: user_fav.user = request.user user_fav.fav_id = int(id) user_fav.fav_type = int(type) user_fav.user = request.user user_fav.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): # 用户收藏之前,先拿到收藏页面类别,和id fav_id = int(request.POST.get('fav_id', 0)) fav_type = int(request.POST.get('fav_type', 0)) # 先判断用户是否登陆 if not request.user.is_authenticated(): # 如果没登陆,则直接返回登陆界面 return HttpResponse('{"status":"fail", "msg":"用户未登陆"}', content_type="application/json") # 去数据库查询是否存在收藏记录 exit_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exit_records: # 如果已经存在,则表示用户想取消收藏 exit_records.delete() # 用户取消收藏数减少操作 if fav_type == 1: course = Course.objects.get(id=fav_id) course.fav_nums -= 1 if course.fav_nums < 0: course.fav_nums = 0 course.save() elif fav_type == 2: course_org = CourseOrg.objects.get(id=fav_id) course_org.fav_nums -= 1 if course_org.fav_nums < 0: course_org.fav_nums = 0 course_org.save() elif fav_type == 3: teacher = Teacher.objects.get(id=fav_id) teacher.fav_nums -= 1 if teacher.fav_nums < 0: teacher.fav_nums = 0 teacher.save() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type="application/json") else: fav_user = UserFavorite() if fav_id > 0 and fav_type > 0: fav_user.user = request.user fav_user.fav_type = fav_type fav_user.fav_id = fav_id fav_user.save() # 用户添加收藏数累加逻辑 if fav_type == 1: course = Course.objects.get(id=fav_id) course.fav_nums += 1 course.save() elif fav_type == 2: course_org = CourseOrg.objects.get(id=fav_id) course_org.fav_nums += 1 course_org.save() elif fav_type == 3: teacher = Teacher.objects.get(id=fav_id) teacher.fav_nums += 1 teacher.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type="application/json") else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type="application/json")
def post(self, request): # print(type(request.POST.get('fav_id', 0)),"*"*100) try: fav_id = int(request.POST.get('fav_id', 0)) fav_type = int(request.POST.get('fav_type', 0)) except Exception as e: return HttpResponse('{"status":"fail","msg":"非法请求"}', content_type='application/json') if not request.user.is_authenticated: return HttpResponse('{"status":"fail","msg":"用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if exist_records: exist_records.delete() self.set_fav_nums(fav_type, fav_id, -1) return HttpResponse('{"status":"success","msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() if fav_id > 0 and fav_type > 0: user_fav.user = request.user user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.save() self.set_fav_nums(fav_type, fav_id, 1) return HttpResponse('{"status":"success","msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) if not request.user.is_authenticated(): # 判断用户登录状态 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_records: # 如果记录已经存在, 则表示用户取消收藏 exist_records.delete() if int(fav_type) == 1: course = Course.objects.get(id=int(fav_id)) course.fav_nums -= 1 if course.fav_nums < 0: course.fav_nums = 0 course.save() elif int(fav_type) == 2: course_org = CourseOrg.objects.get(id=int(fav_id)) course_org.fav_nums -= 1 if course_org.fav_nums < 0: course_org.fav_nums = 0 course_org.save() elif int(fav_type) == 3: teacher = Teacher.objects.get(id=int(fav_id)) teacher.fav_nums -= 1 if teacher.fav_nums < 0: teacher.fav_nums = 0 teacher.save() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() if int(fav_type) == 1: course = Course.objects.get(id=int(fav_id)) course.fav_nums += 1 course.save() elif int(fav_type) == 2: course_org = CourseOrg.objects.get(id=int(fav_id)) course_org.fav_nums += 1 course_org.save() elif int(fav_type) == 3: teacher = Teacher.objects.get(id=int(fav_id)) teacher.fav_nums += 1 teacher.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) user = request.user resp = {} # 判断是否登录 if not request.user.is_authenticated(): resp['status'] = 'fail' resp['msg'] = '用户未登录' return HttpResponse(json.dumps(resp), content_type="application/json") else: exist_fav = UserFavorite.objects.filter(user=user, fav_id=fav_id, fav_type=fav_type) # 判断是否收藏 if exist_fav: # 取消收藏 resp['status'] = 'success' resp['msg'] = '收藏' exist_fav.delete() return HttpResponse(json.dumps(resp), content_type="application/json") else: # 收藏 user_fav = UserFavorite() user_fav.user = user user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.save() resp['status'] = 'success' resp['msg'] = '已收藏' return HttpResponse(json.dumps(resp), content_type="application/json")
def post(self, request): fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) ''' 判断用户是否登录 ''' if not request.user.is_authenticated(): return HttpResponse("{'status':'fail','msg':'用户未登录'}", content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_records: #记录已经存在,则表示用户取消收藏 exist_records.delete() return HttpResponse("{'status':'success','msg':'收藏'}", content_type='application/json') else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() return HttpResponse("{'status':'success','msg':'已收藏'}", content_type='application/json') else: return HttpResponse("{'status':'fail','msg':'收藏出错'}", content_type='application/json')
def post(self, request): fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) # 判断用户登录状态 if not request.user.is_authenticated: return HttpResponse('{"status":"fail","msg":"用户未登录"}', content_type="application/json") exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_records: # 如果用户已收藏,则认为此动作是取消收藏 exist_records.delete() return HttpResponse('{"status":"fail","msg":"收藏"}', content_type="application/json") else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type="application/json") else: return HttpResponse('{"status":"fail", "msg":"收藏错误"}', content_type="application/json")
def post(self, request): fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) if not request.user.is_authenticated(): return HttpResponse('{"status":"fail","msg":"用户未登录"}', content_type='application/json') exist_record = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_record: #存在记录则取消 exist_record.delete() return HttpResponse('{"status":"success","msg":"收藏"}', content_type="application.json") else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.fav_type = fav_type user_fav.fav_id = fav_id user_fav.user = request.user user_fav.save() return HttpResponse('{"status":"success","msg":"已收藏"}', content_type="application.json") else: return HttpResponse('{"status":"fail","msg":"收藏出错"}', content_type="application.json") pass
def post(self, request): fav_id = int(request.POST.get('fav_id', 0)) fav_type = int(request.POST.get('fav_type', 0)) ##首先判断用户是否登录 if not request.user.is_authenticated(): return HttpResponse('{"status":"fail","msg":"用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type) if exist_records: ##记录已经存在,则取消收藏 exist_records.delete() return HttpResponse('{"status":"success","msg":"点击收藏"}', content_type='application/json') else: user_fav = UserFavorite() if fav_id > 0 and fav_type > 0: user_fav.user = request.user user_fav.fav_id = fav_id user_fav.fav_type = fav_type user_fav.save() return HttpResponse('{"status":"success","msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail","msg":" fail"}', content_type='application/json')
def post(self, request): fav_id = request.POST.get('fav_id', 0) fav_type = request.POST.get('fav_type', 0) if not request.user.is_authenticated: return HttpResponse('{"status": "fail", "msg": "用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) # 取消收藏 if exist_records: exist_records.delete() self.delete_fav(fav_id, fav_type) return HttpResponse('{"status": "success", "msg": "已取消收藏"}', content_type='application/json') # 收藏 elif fav_id and fav_type: user_favorite = UserFavorite() user_favorite.user = request.user user_favorite.fav_id = fav_id user_favorite.fav_type = fav_type user_favorite.save() self.insert_fav(fav_id, fav_type) return HttpResponse('{"status": "success", "msg": "已收藏"}', content_type='application/json') else: return HttpResponse('{"status": "fail", "msg": "收藏错误"}', content_type='application/json')
def post(self, request): id = request.POST.get('fav_id', 0) # 防止后边int(fav_id)时出错 type = request.POST.get('fav_type', 0) # 防止int(fav_type)出错 if not request.user.is_authenticated: # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_record = UserFavorite.objects.filter(user=request.user, fav_id=int(id), fav_type=int(type)) if exist_record: # 如果记录已经存在,表示用户取消收藏 exist_record.delete() if int(type) == 1: course = Course.objects.get(id=int(id)) course.fav_nums -= 1 if course.fav_nums < 0: course.fav_nums = 0 course.save() elif int(type) == 2: org = CourseOrg.objects.get(id=int(id)) org.fav_nums -= 1 if org.fav_nums < 0: org.fav_nums = 0 org.save() elif int(type) == 3: teacher = Teacher.objects.get(id=int(id)) teacher.fav_nums -= 1 if teacher.fav_nums < 0: teacher.fav_nums = 0 teacher.save() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(type) > 0 and int(id) > 0: user_fav.fav_id = int(id) user_fav.fav_type = int(type) user_fav.user = request.user user_fav.save() if int(type) == 1: course = Course.objects.get(id=int(id)) course.fav_nums += 1 course.save() elif int(type) == 2: org = CourseOrg.objects.get(id=int(id)) org.fav_nums += 1 org.save() elif int(type) == 3: teacher = Teacher.objects.get(id=int(id)) teacher.fav_nums += 1 teacher.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = request.POST.get('fav_id', 0) if not request.user.is_authenticated: # 判断用户登录状态 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id)) if exist_records: # 如果记录已经存在, 则表示用户取消收藏 exist_records.delete() competition = Competition.objects.get(id=int(fav_id)) competition.fav_nums -= 1 if competition.fav_nums < 0: competition.fav_nums = 0 competition.save() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(fav_id) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.save() competition = Competition.objects.get(id=int(fav_id)) competition.fav_nums += 1 competition.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self,request): fav_id = request.POST.get('fav_id',0) fav_type = request.POST.get('fav_type',0) # 首先要判断用户登陆状态 if not request.user.is_authenticated(): return HttpResponse('{"status":"success","msg":"用户未登录"}', content_type='application/json') exist_records = UserFavorite.objects.filter(user=request.user,fav_type=int(fav_type), fav_id=int(fav_id)) if exist_records: # 取消收藏 exist_records.delete() if int(fav_type) == 1: course = Courses.objects.get(id = int(fav_id)) course.fav_nums -= 1 if course.fav_nums < 0: course.fav_nums = 0 course.save() elif int(fav_type) == 2: course_org = CourseOrg.objects.get(id = int(fav_id)) course_org.fav_nums -= 1 if course_org.fav_nums < 0: course_org.fav_nums = 0 course_org.save() elif int(fav_type) == 3: teacher = Teacher.objects.get(id = int(fav_id)) teacher.fav_nums -= 1 if teacher.fav_nums < 0: teacher.fav_nums = 0 teacher.save() return HttpResponse('{"status":"success","msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) >0 : user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() if int(fav_type) == 1: course = Courses.objects.get(id=int(fav_id)) course.fav_nums += 1 course.save() elif int(fav_type) == 2: course_org = CourseOrg.objects.get(id=int(fav_id)) course_org.fav_nums += 1 course_org.save() elif int(fav_type) == 3: teacher = Teacher.objects.get(id=int(fav_id)) teacher.fav_nums += 1 teacher.save() return HttpResponse('{"status":"success","msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = request.POST.get('fav_id', 0) # 防止后边int(fav_id)时出错 fav_type = request.POST.get('fav_type', 0) # 防止int(fav_type)出错 if not request.user.is_authenticated: # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_record = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_record: # 如果记录已经存在,表示用户取消收藏 exist_record.delete() if int(fav_type) == 1: course = Course.objects.get(id=int(fav_id)) course.fav_nums -= 1 if course.fav_nums < 0: course.fav_nums = 0 course.save() elif int(fav_type) == 2: org = CourseOrg.objects.get(id=int(fav_id)) org.fav_nums -= 1 if org.fav_nums < 0: org.fav_nums = 0 org.save() elif int(fav_type) == 3: teacher = Teacher.objects.get(id=int(fav_id)) teacher.fav_nums -= 1 if teacher.fav_nums < 0: teacher.fav_nums = 0 teacher.save() return HttpResponse('{"status":"success", "msg":"收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(fav_type) > 0 and int(fav_id) > 0: user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.user = request.user user_fav.save() if int(fav_type) == 1: course = Course.objects.get(id=int(fav_id)) course.fav_nums += 1 course.save() elif int(fav_type) == 2: org = CourseOrg.objects.get(id=int(fav_id)) org.fav_nums += 1 org.save() elif int(fav_type) == 3: teacher = Teacher.objects.get(id=int(fav_id)) teacher.fav_nums += 1 teacher.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')
def post(self, request): fav_id = request.POST.get('fav_id', 0) # 防止后边int(fav_id)时出错 fav_type = request.POST.get('fav_type', 0) # 防止int(fav_type)出错 if not request.user.is_authenticated: # 未登录时返回json提示未登录,跳转到登录页面是在ajax中做的 return HttpResponse('{"status":"fail", "msg":"用户未登录"}', content_type='application/json') exist_record = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type)) if exist_record: # 如果记录已经存在,表示用户取消收藏 exist_record.delete() return HttpResponse('{"status":"fail", "msg":"已取消收藏"}', content_type='application/json') else: user_fav = UserFavorite() if int(fav_id) > 0 and int(fav_type) > 0: user_fav.user = request.user user_fav.fav_id = int(fav_id) user_fav.fav_type = int(fav_type) user_fav.save() return HttpResponse('{"status":"success", "msg":"已收藏"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"收藏出错"}', content_type='application/json')