收藏与取消收藏:就是点击这个:@click="store(item.content,item.star_style)"
通过点击的item.content传给后端判断当前点击的这条目是什么
展示星星的颜色与条目信息是在onload函数里的。
发帖
就近帮扶
{{item.content}}
接单
Django
接收内容,拿到当前用户信息与要收藏的条目
然后设置User和Fornum这两个模型类的多对多映射的
然后把当前的条目跟当前用户关联起来
def store(request):
try:
po = json.loads(request.body.decode("utf-8"))
content = po['content']
user = po['user']
sign = po['sign']
obj = Forum.objects.get(content=content)
us = User.objects.get(username=user)
if sign == '0':
us.forum_set.add(obj) # 直接把这个用户名和这个帖子关联上
else:
us.forum_set.remove(obj)
except Exception:
return JsonResponse({'code': 1000, 'error': '出错了'})
return JsonResponse({'code': 200})
然后实现收藏后星星变黄,再次点击星星变白,消除原先的映射关系(这里把帖子错写成star_books了,只是名字问题)
就是把有关联的条目都找出来,然后换一下star_style
def show_fornum(request):
liss = []
po = json.loads(request.body.decode("utf-8"))
path = po['path']
lis = path.split('/')
Type = lis[-1]
username = po['user']
informations = Forum.objects.filter(type=Type)
# 首先找到当前用户对象
user = User.objects.get(username=username)
# 然后找出这个用户收藏的所有条目
star_books = user.forum_set.all()
for information in informations:
# 该用户收藏的,star_style是黄色。没收藏的是白色
if information not in star_books:
liss.append({
'content': information.content,
'star_style': '../../../static/start.png'
})
else:
liss.append({
'content': information.content,
'star_style': 'https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com'
'/621b3ba25a7e3f031094e05a/6263de904eb5590011d34380/16514938790353679819.png '
})
return JsonResponse({
'informations': liss
})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)