def cais(request, response, content): if request.method == "GET": #查询 if request.GET: food_id = request.GET["food_id"] f = Food.objects.filter(food_id=food_id).first() info = {"name": f.name, "price": str(f.price)} content["info"] = info else: foods = Food.objects.filter( is_delete=0, create_time__day=datetime.today().strftime("%d")) # foods = Food.objects.filter(is_delete=0,create_time__day="28") print ">>>foods: ", foods info = [{ "name": f.name, "price": str(f.price), "time": f.create_time.strftime("%Y-%m-%d %H:%M:%S") } for f in foods] content["info"] = info elif request.method == "POST": #添加 group_id = request.POST["group_id"] store_id = request.POST["store_id"] name = request.POST["name"] desc = request.POST["desc"] food_img = request.POST["food_img"] price = request.POST["price"] sequence = int(request.POST.get("sequence", 1)) is_delete = int(request.POST.get("is_delete", 0)) food = Food() food.food_id = rand_str(8) food.group_id = group_id food.store_id = store_id food.name = name food.desc = desc food.food_img = food_img food.price = change_money(price) food.sequence = sequence food.is_delete = is_delete food.save() content["info"] = {"msg": "成功"} elif request.method == "PUT": #修改 put = QueryDict(request.body) food_id = put["food_id"] name = put["name"] food = Food.objects.filter(food_id=food_id, is_delete=0).first() food.name = name food.save() info = {"msg": "更新名字成功", "name": name} content["info"] = info elif request.method == "DELETE": #删除 delete = QueryDict(request.body) food_id = delete["food_id"] food = Food.objects.filter(food_id=food_id, is_delete=0).first() food.is_delete = 1 food.save() info = {"msg": "菜删除成功"} content["info"] = info
def post(self, request): try: print ">>>request.POST", request.POST food_img = request.POST["food_img"] # 菜图片 food_price = request.POST["food_price"] # 菜价格 food_name = request.POST["food_name"] # 菜名字 food_desc = request.POST["food_desc"] # 活动配图 group_id = request.POST["group_id"] # 分组id food_id = rand_str(8) food = Food() food.food_id = food_id food.name = food_name food.group_id = group_id food.food_img = food_img food.desc = food_desc food.price = food_price food.create_time = datetime.now() food.save() return JsonResponse({"status": 200, "data": request.POST}) except BaseException, e: return JsonResponse({"error": str(e), "status": 400})