广告小程序后端开发(11.个人中心:获取用户数据,用户发布的广告和店铺及图片)
1.个人中心:获取用户身份数据
1.apps/users/adminx.py中对UserProfile的序列化类进行修改为:
class UserProfileModelSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields=['nickName','avatarUrl','jifen','yongjin','fensi_num']
2.apps/users/views.py中新建获取用户信息类GetUserInfo:
class GetUserInfo(APIView):
"""获取用户信息"""
renderer_classes = [JSONRenderer] # 渲染器
def get(self,request):
token=request.GET.get('token')
user=UserProfile.objects.filter(token=token).first()
re=UserProfileModelSerializer(user)
return Response(re.data)
3.在apps/users/urls.py中添加路由:
from django.urls import path
from .views import LoginView,GetUserInfo
urlpatterns = [
path('login/',LoginView.as_view()),
path('getuserinfo/',GetUserInfo.as_view())
]
2.个人中心:获取用户发布的广告和店铺及图片
1.在apps/ad/views.py中新建获取用户发布的广告和店铺及图片的类GetUserGoods:
class GetUserGoods(APIView): """获取用户发布的广告或店铺,以及每个广告或店铺的第一张图片""" renderer_classes = [JSONRenderer] # 渲染器 def get(self, request): token = request.GET.get('token') user = UserProfile.objects.filter(token=token).first() if user: re_list=Good.objects.filter(user=user).order_by('-add_time') re = GoodModelSerializer(re_list, many=True) image_list = [] img = {} for item1 in re_list: first_img = Images.objects.filter(good=item1).first() if first_img: img['id'] = item1.id img['image'] = HOST + str(first_img.image) image_list.append(img) img = {} # 没有这一句,最后图片列表中所有元素都会是最后一次对img的赋值内容 return Response(data=({'good': re.data, 'image_list': image_list})) else: return HttpResponse('无此用户')
2.在apps/ad/urls.py中配置路由:
from django.urls import path from .views import GetCategory,CreateAdOrShop,UpLoadImg,GetPubGoods,GetGoodDesc,GetUserGoods urlpatterns = [ path('getcategory/',GetCategory.as_view()), path('createadorshop/',CreateAdOrShop.as_view()), path('uploadimg/',UpLoadImg.as_view()), path('getpubgoods/',GetPubGoods.as_view()), path('getgooddesc/',GetGoodDesc.as_view()), path('getusergoods/',GetUserGoods.as_view()), ]