请求的是购物网站的分类后产品数据。
说明 | 详情 |
---|---|
接口名 | classify |
作用 | 返回请求的分类后的产品的Json格式数据 |
调用url | api/ |
请求方法 | Post |
请求参数 | “type” :整数 0 为 热门,1为家居 ”page”:整数页码 |
返回参数 | “status” = 200 : 成功 10021 : 查询列表为空 10022:分页后产品为空 |
因为尝试些API接口,所以在目录下新建了一个views文件;
from django.conf.urls import url
from taobao import views, views_api
urlpatterns = [
url(r'^$', views.index),
url(r'^api/',views_api.classify)
]
from django.db import models
class goods(models.Model):
category = models.IntegerField('分类',default=0)
goods_id = models.CharField('商品ID',max_length=10)
goods_name = models.CharField('商品名',max_length=100,default='')
goods_price = models.DecimalField('商品价格',max_digits=10,decimal_places=2)
goods_Stock = models.IntegerField('商品库存',default=100)
sales_Volume = models.IntegerField('销量',default=0)
goods_introduce = models.CharField('商品简介',max_length=250,default='')
def __str__(self):
return self.goods_name
from taobao.models import goods
from django.http import JsonResponse
from django.core.paginator import Paginator ,PageNotAnInteger ,EmptyPage
from django.views.decorators.csrf import csrf_exempt
#分类展示
#测试的时候我先把csrf关了
@csrf_exempt
def classify(req):
context ={'status':200}
#获取 post 数据
type = req.POST.get('type','')
page = req.POST.get('page','')
context['type'] , context['page'] = type ,page
#根据类别在数据库找到相应数据
if type == '0':
goods_list = goods.objects.order_by('sales_Volume').all()
else:
goods_list = goods.objects.all().filter(category = int(type)).order_by('sales_Volume').all()
#数据为空直接返回json
if goods_list == None:
return JsonResponse({'status':10021,'message':'parameter error'})
#分页
paginator = Paginator(goods_list,8)
try:
goodss = paginator.page(int(page))
except PageNotAnInteger:
goodss = paginator.page(1)
except EmptyPage:
goodss = paginator.page(paginator.num_pages)
#一页商品的个数,是否有前一页,是否有后一页
context['queryNum'],context['hasPrevios'],context['hasNext'] = len(goodss),goodss.has_previous(),goodss.has_next()
#将数据存入data[]
data = []
if goodss:
for i in goodss:
good = {}
good['goods_id'] = i.goods_id
good['goods_name'] = i.goods_name
good['goods_price'] = i.goods_price
good['goods_stock'] = i.goods_Stock
good['sales_volume'] = i.sales_Volume
good['goods_introduce'] = i.goods_introduce
data.append(good)
# 将data存进context
context.update({'data':data})
# 返回json
return JsonResponse(context)
else:
return JsonResponse({'status':10022,'message':'query goods isempty'})
这里使用到postman;
用chrome浏览器添加扩展程序Postman,并安装桌面应用。
应用页面如下:
{
"status": 200,
"type": "1",
"page": "1",
"queryNum": 8,
"hasPrevios": false,
"hasNext": true,
"data": [
{
"goods_id": "33428",
"goods_name": " 岷江音乐 三角调配茶包 8枚",
"goods_price": "45.00",
"goods_stock": 100,
"sales_volume": 0,
"goods_introduce": " 音乐和茶 伴你左右 "
},
{
"goods_id": "33431",
"goods_name": " 【预售】榨冰+沙拉一体机",
"goods_price": "249.00",
"goods_stock": 100,
"sales_volume": 0,
"goods_introduce": " 沙冰或沙拉 它都能搞定 "
},
{
"goods_id": "32755",
"goods_name": " 特级初榨橄榄油 500ml 8色可选,2种口味,意大利纯手工制罐,古法压榨油",
"goods_price": "299.00",
"goods_stock": 100,
"sales_volume": 0,
"goods_introduce": " 橄榄油奥斯卡得主 "
},
{
"goods_id": "31226",
"goods_name": " Ball Mason Jar 梅森罐 玻璃密封罐 蔬菜沙拉罐 12oz水晶杯 360ml",
"goods_price": "39.00",
"goods_stock": 100,
"sales_volume": 0,
"goods_introduce": " 美观实用合二为一 "
},
{
"goods_id": "33411",
"goods_name": " 土耳其手工玻璃贝壳盘 2件套",
"goods_price": "138.00",
"goods_stock": 100,
"sales_volume": 0,
"goods_introduce": " 手工上色 匠心之作 "
},
{
"goods_id": "32998",
"goods_name": " 梅森瓶蔬果榨汁机 含两个梅森瓶",
"goods_price": "498.00",
"goods_stock": 100,
"sales_volume": 0,
"goods_introduce": " 营养果汁 随时打包 "
},
{
"goods_id": "33409",
"goods_name": " 土耳其手工璃鱼盘 3件套",
"goods_price": "169.00",
"goods_stock": 100,
"sales_volume": 0,
"goods_introduce": " 把海洋搬到餐桌上 "
},
{
"goods_id": "33416",
"goods_name": " 小确幸调味花果茶 泡袋茶 9枚装",
"goods_price": "58.00",
"goods_stock": 100,
"sales_volume": 0,
"goods_introduce": " 每天都多一份小确幸 "
}
]
}