def addGoods(request): goods = Goods() goods.g_name = 'iphone-' + str(random.randrange(1, 20)) goods.g_price = random.randrange(9999, 20000) goods.save() return HttpResponse('添加商品成功: ' + goods.g_name)
def post(self): # g_name = request.form.get('g_name') # g_price = request.form.get('g_price') # 使用request.form获取的参数无校验功能 args = parser.parse_args() g_name = args.get('g_name') g_price = args.get('g_price') print(args.get('mu')) # 当该参数设置了append的action后get到的就是一个list print(args.get('User-Agent')) goods = Goods() goods.g_name = g_name goods.g_price = g_price if not goods.save(): abort(400) # data = { # 'status': 200, # 'msg': 'create success', # 'data': marshal(goods, good_fields), # } data = { 'status': 200, 'msg': 'create success', 'data': goods, # 'test': 'haha' # 如果这个字段在fields中没有,则最后返回的结果会被忽略这个字段的内容 } return data
def addgoods(request): goods = Goods() names = ['小米', '锤子', '红米', 'oppo', '华为', '魅族', '魅蓝'] temp = random.randrange(0, len(names)) goods.g_name = names[temp] + '-' + str(random.randrange(10, 100)) goods.g_price = random.randrange(100, 1000) goods.save() return HttpResponse('添加商品成功')
def addgoods(request): goods = Goods() arr = ['iPhone', 'iPad', 'iPod', 'MacBook Pro', 'MacBook Air'] temp = random.randrange(0, len(arr)) goods.g_name = arr[temp] + '-' + str(random.randrange(0, 10)) goods.g_price = random.randrange(10000, 100000) goods.save() return HttpResponse('添加商品成功')
def post(self): # g_name=request.form.get('g_name') # g_price=request.form.get('g_price') args = parser.parse_args() g_name = args.get('g_name') g_price = args.get('g_price') goods = Goods() goods.g_name = g_name goods.g_price = g_price if not goods.save(): abort(404) data = {"msg": "create success", "status": 201, "data": goods} return data