广告小程序后端开发(9.获取已发布的广告和店铺列表,获取已发布的广告和店铺详情)
1.获取已发布的广告和店铺列表
1.在apps/ad/views.py中编写 获取已发布的广告或店铺列表类GetPubGoods:
class GetPubGoods(APIView): """获取已发布的广告或店铺列表""" renderer_classes = [JSONRenderer] # 渲染器 def get(self,request): page=request.GET.get('page') sheng=request.GET.get('sheng') shi = request.GET.get('shi') xian = request.GET.get('xian') tab=request.GET.get('tab') tab1 = request.GET.get('tab1') if page and tab and tab1 and xian and shi and sheng: # print(page,sheng,shi,xian,tab,tab1) page=int(page) tab=int(tab) tab1=int(tab1) fenlei_id=request.GET.get('fenleiid') if fenlei_id: # 分类页面发来的请求 fenlei_id=int(fenlei_id) category=Category.objects.filter(id=fenlei_id).first() if tab1 == 1: # 全省 num = Area.objects.filter(name=sheng).first().num # print(num) re_list = Good.objects.filter(area__num__gt=num, area__num__lt=(num + 10000), stage=2, type=tab,category=category).order_by('-last_visit_time')[0:(5 * page)] re = GoodModelSerializer(re_list, many=True) image_list = [] img = {} for item1 in re_list: first_img = Images.objects.filter(good=item1).first() 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})) elif tab1 == 2: # 全市 num = Area.objects.filter(name=shi).first().num # print(num) re_list = Good.objects.filter(area__num__gte=num, area__num__lt=(num + 100), stage=2, type=tab,category=category).order_by('-last_visit_time')[0:(5 * page)] re = GoodModelSerializer(re_list, many=True) image_list = [] img = {} for item1 in re_list: first_img = Images.objects.filter(good=item1).first() 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})) elif tab1 == 3: # 全县 num = Area.objects.filter(name=xian).first().num # print(num) re_list = Good.objects.filter(area__num=num, stage=2, type=tab,category=category).order_by('-last_visit_time')[ 0:(5 * page)] re = GoodModelSerializer(re_list, many=True) image_list = [] img = {} for item1 in re_list: first_img = Images.objects.filter(good=item1).first() 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('tab1参数异常') else: # 首页发起的请求 if tab1==1: # 全省 num=Area.objects.filter(name=sheng).first().num # print(num) re_list=Good.objects.filter(area__num__gt=num,area__num__lt=(num+10000),stage=2,type=tab).order_by('-last_visit_time')[0:(5 * page)] re = GoodModelSerializer(re_list, many=True) image_list=[] img={} for item1 in re_list: first_img=Images.objects.filter(good=item1).first() 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})) elif tab1==2: #全市 num = Area.objects.filter(name=shi).first().num # print(num) re_list = Good.objects.filter(area__num__gte=num, area__num__lt=(num + 100), stage=2,type=tab).order_by('-last_visit_time')[0:(5 * page)] re = GoodModelSerializer(re_list, many=True) image_list = [] img = {} for item1 in re_list: first_img=Images.objects.filter(good=item1).first() 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})) elif tab1==3: #全县 num = Area.objects.filter(name=xian).first().num # print(num) re_list = Good.objects.filter(area__num=num, stage=2,type=tab).order_by('-last_visit_time')[0:(5 * page)] re = GoodModelSerializer(re_list, many=True) image_list = [] img = {} for item1 in re_list: first_img=Images.objects.filter(good=item1).first() 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('tab1参数异常') else: return HttpResponse('参数不足')
2.在apps/ad/urls.py中配置路由:
from django.urls import path from .views import GetCategory,CreateAdOrShop,UpLoadImg,GetGoods,GetImgs,GetPubGoods urlpatterns = [ path('getcategory/',GetCategory.as_view()), path('createadorshop/',CreateAdOrShop.as_view()), path('uploadimg/',UpLoadImg.as_view()), path('getpubgoods/',GetPubGoods.as_view()), ]
2.获取已发布的广告和店铺详情
1.在apps/ad/views.py中编写 获取广告或店铺详情类 GetGoodDesc:
#…… from datetime import datetime #…… class GetGoodDesc(APIView): """获取广告或店铺详情""" renderer_classes = [JSONRenderer] # 渲染器 def get(self,request): good_id=request.GET.get('goodid') if good_id: good_id=int(good_id) good=Good.objects.filter(id=good_id).first() good.last_visit_time=datetime.now() good.pv+=1 good.save() re_good=GoodModelSerializer(good) img_obj_list=Images.objects.filter(good=good) img_list=[] for item in img_obj_list: img_list.append(HOST+str(item.image)) return Response(data=({'good':re_good.data,'image_list':img_list})) return HttpResponse('未收到id')
2.在apps/ad/urls.py中编写路由:
from django.urls import path from .views import GetCategory,CreateAdOrShop,UpLoadImg,GetGoods,GetImgs,GetPubGoods,GetGoodDesc 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()), ]