项目实训5

第二周第二次

优化了之前代码的逻辑,增加了判断错误以及错误返回信息。

代码改进:

class CropaView(APIView):
    def post(self, request):
        try:
            cropinput = request.data
            crops = models.Crop.objects.filter(city=cropinput["city"], crop_type=cropinput["type"])
            if not crops:
                return JsonResponse({'message': '查询城市作物不存在'}, status=404)
                crop_list = []
                for crop in crops:
                    crop_dict = {

                        'city': crop.city,
                        'crop_type': crop.crop_type,
                        'weight_all': crop.weight_all
                    }
                    crop_list.append(crop_dict)
                return JsonResponse(crop_list, safe=False)
        except Exception as e:
            return JsonResponse({'message': '请求异常'})

之后将相应的后端功能进行同样的判断错误情况优化


实现了相应的接口里的功能:

实现效果如下:

代码实现:

class CropCompareView(APIView):
    def post(self, request):
        try:
            cropinput = request.data
            crops1 = models.Crop.objects.filter(city=cropinput["city1"], crop_type=cropinput["type"],
                                                year=cropinput["year"])
            crops2 = models.Crop.objects.filter(city=cropinput["city2"], crop_type=cropinput["type"],
                                                year=cropinput["year"])
            if not crops1:
                return JsonResponse({'message': '城市1该年不存在作物'})
            if not crops2:
                return JsonResponse({'message': '城市2该年不存在作物'})
            for crop1 in crops1:
                for crop2 in crops2:
                    crop_dict = {
                        'city1': crop1.city,
                        'city2': crop2.city,
                        'crop_type': crop1.crop_type,
                        'year': crop1.year,
                        'area1': crop1.area,
                        'weight_all1': crop1.weight_all,
                        'weight_avg1': crop1.weight_avg,
                        'area2': crop2.area,
                        'weight_all2': crop2.weight_all,
                        'weight_avg2': crop2.weight_avg,
                    }

            return JsonResponse(crop_dict, safe=False)
        except Exception as e:
            return JsonResponse({'message': '请求异常'})

class TemperatureCompareView(APIView):
    def post(self, request):
        try:
            climateinput = request.data
            climates1 = models.Climate.objects.filter(city=climateinput["city1"], year=climateinput["year"])
            climates2 = models.Climate.objects.filter(city=climateinput["city2"], year=climateinput["year"])
            if not climates1:
                return JsonResponse({'message': '城市1该年数据不存在'})
            if not climates2:
                return JsonResponse({'message': '城市2该年数据不存在'})
            cliamte_list = []
            for climate1 in climates1:
                for climate2 in climates2:
                    if climate1.month==climate2.month:
                        cliamte_dict = {
                            'city1': climate1.city,
                            'city2': climate2.city,
                            'year': climate1.year,
                            'month': climate1.month,
                            'temperature1': climate1.temperature,
                            'temperature2': climate2.temperature,
                        }
                        cliamte_list.append(cliamte_dict)
            return JsonResponse(cliamte_list, safe=False)
        except Exception as e:
            return JsonResponse({'message': '请求异常'})

class RainCompareView(APIView):
    def post(self, request):
        try:
            climateinput = request.data
            climates1 = models.Climate.objects.filter(city=climateinput["city1"], year=climateinput["year"])
            climates2 = models.Climate.objects.filter(city=climateinput["city2"], year=climateinput["year"])
            if not climates1:
                return JsonResponse({'message': '城市1该年数据不存在'})
            if not climates2:
                return JsonResponse({'message': '城市2该年数据不存在'})
            cliamte_list = []
            for climate1 in climates1:
                for climate2 in climates2:
                    if climate1.month==climate2.month:
                        cliamte_dict = {
                            'city1': climate1.city,
                            'city2': climate2.city,
                            'year': climate1.year,
                            'month': climate1.month,
                            'rain1': climate1.rain,
                            'rain2': climate2.rain,
                        }
                        cliamte_list.append(cliamte_dict)
            return JsonResponse(cliamte_list, safe=False)
        except Exception as e:
            return JsonResponse({'message': '请求异常'})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值