# 收藏 def star(request): hid = request.POST.get('hid') uid = int(request.COOKIES.get('uid', -1)) if Star.objects.filter(user_id=int(uid), stock_id=int(hid)): Star.objects.filter(user_id=int(uid), stock_id=int(hid)).delete() return JsonResponse({'color': 'black', 'msg': '取消收藏啦'}) else: Star.objects.create(user_id=int(uid), stock_id=int(hid)) return JsonResponse({'color': 'red', 'msg': '收藏啦'})
时间: 2023-12-12 19:04:37 浏览: 139
这是一个 Django Web 框架中的视图函数,用于处理用户的收藏操作。它的输入参数是一个 request 对象,该对象包含了用户的请求信息,包括请求方法、请求参数等。该视图函数首先从请求参数中获取 hid 和 uid,分别指代用户要收藏的股票的 id 和用户的 id。然后它会在数据库中查询该用户是否已经收藏了该股票,如果已经收藏,则会删除该收藏记录并返回一个 JSON 格式的响应,表示取消收藏成功;如果未收藏,则会在数据库中创建一条新的收藏记录,并同样返回一个 JSON 格式的响应,表示收藏成功。该视图函数可以与前端页面中的 JavaScript 代码配合使用,实现用户收藏操作的交互效果。
阅读全文
相关推荐


















