def query_weather(request): if request.method == 'GET': city = request.GET.get('city') data = juhe.weather(city) elif request.method == 'POST': received_body = request.body received_body = json.loads(received_body) cities = received_body.get('cities') response_data = [] for city in cities: result = juhe.weather(city) result['city'] = city response_data.append(result) return JsonResponse(data=response_data, safe=False, status=200)
def weather(request): if request.method == "GET": city = request.GET.get("city") data = juhe.weather(city) return JsonResponse(data=data, status=200) elif request.method == "POST": received_body = request.body received_body = json.loads(received_body) cities = received_body.get("cities") response_data = [] for city in cities: result = juhe.weather(city) result['city'] = city response_data.append(result) return JsonResponse(data=response_data, safe=False, status=200)
def post(self, request): received_body = request.body() received_body = json.load(received_body) movies = received_body.get('title') for mov in movies: result = juhe.weather(mov) response_data.append(result) response_data = self.wrap_json_response(data=response_data) return JsonResponse(data=response_data, safe=False, status=200)
def weather(request): if request.method == 'GET': city = request.GET.get('city') data = juhe.weather(city) return JsonResponse(data=data, safe=False, status=200) elif request.method == 'POST': recived_body = request.body recived_body = json.loads(recived_body) cities = recived_body['cities'] response_data = [] for item in cities: province = item['province'] city = item['city'] area = item['area'] query = area + ',' + city + ',' + province result = juhe.weather(query) response_data.append(result) return JsonResponse(data=response_data, safe=False, status=200)
def ha_request(cls, cityname, timeout): try: data = juhe.weather(cityname, timeout) except Exception as e: logger.error( "Request juhe weather API timeout. HARequest switch to hefeng weather." ) data = heweather.HeWeather.get_weather(cityname, timeout) # data = data.to_dict() return data
def weather(request): if request.method == 'GET': city = request.GET.get('city') #直接从请求头中的参数取出来 data = juhe.weather(city) return JsonResponse(data=data, safe=False, status=200) elif request.method == 'POST': # print(request.body) #POST方法中,参数是放在body中的,先取出body,是json格式,然后转换成dict,再从dict中取出相关值。 received_body = simplejson.loads(request.body) cities = received_body.get('cities') # print(cities) #返回给前端的数据,先构建一个数组,数组中的每个值是一个dict,返回给前端时再转换成json数据。 response_data = [] for city in cities: # print(city) result = juhe.weather(city) response_data.append(result) print(response_data) return JsonResponse(data=response_data, safe=False, status=200)
def post(self, request): data = [] receive_body = request.body.decode('utf-8') # 请求体,以utf-8解码 receive_body = json.loads(receive_body) # 返回一个dict对象 print(receive_body) cities = receive_body.get('cities') # 请求体里面不止有一个城市 for city in cities: result = juhe.weather(city.get('city')) # city.get('city')获取城市名,传入函数中,返回城市具体天气信息 result['city_info'] = city # 返回前端传进来的城市信息 data.append(result) response_data = self.wrap_json_response(data) return JsonResponse(data=response_data, status=200, safe=False)
def post(self, request): data = [] received_body = request.body.decode('utf-8') received_body = json.loads(received_body) cities = received_body.get('cities') for city in cities: result = juhe.weather(city.get('city')) result['city_info'] = city data.append(result) print(data) response_data = self.wrap_json_response(data=data) return JsonResponse(data=response_data, safe=False)
def post(self, request): """使用post来响应天气查询,在body中填写城市信息更加容易""" data = [] received_body = request.body.decode('utf-8') # 获取body中的数据 received_body = json.loads(received_body) # 使用json标准库加载数据 cities = received_body.get('cities') # 获取post过来的信息data数据 for city in cities: result = juhe.weather(city.get('city')) result['city_info'] = city data.append(result) data = self.wrap_json_response(data=data) # 从Mixin继承来的方法 return JsonResponse(data=data, safe=False)
def get(self, request): if not utils.auth.already_authorized(request): response = self.wrap_json_response({}, code=ReturnCode.UNAUTHORIZED) else: data = [] open_id = request.session.get('open_id') user = User.objects.filter(open_id=open_id)[0] cities = json.loads(user.focus_cities) for city in cities: result = juhe.weather(city.get('city')) result['city_info'] = city data.append(result) response = self.wrap_json_response(data=data, code=ReturnCode.SUCCESS) return JsonResponse(data=response, safe=False)
def post(self, request): recived_body = request.body.decode('utf-8') recived_body = json.loads(recived_body) cities = recived_body['cities'] response_data = [] for item in cities: province = item['province'] city = item['city'] area = item['area'] print('province:', province, 'city:', city, 'area:', area) result = juhe.weather(city, area) response_data.append(result) response = self.wrap_json_response(data=response_data) return JsonResponse(data=response, safe=False)
def ha_request(cls, cityname, timeout): """ :param cityname: :param timeout:利用该参数判断请求是否发生故障 :return: """ try: data = juhe.weather(cityname, timeout) except Exception as e: # 如果发生异常就说明是请求超时了,便去请求heweather的数据 logger.error("Request juhe weather API timeout. " "HARequest switch to hefeng weather.") data = heweather.HeWeather.get_weather(cityname, timeout) return data
def post(self, request): body = request.body json_body = json.loads(body) cities = json_body.get('cities') result = [] for item in cities: city = item['city'] print(city) data_city = juhe.weather(city) data_city['city'] = city result.append(data_city) print(result) data = self.wrap_json_response(data=result) return JsonResponse(data=data, safe=False)
def get(self, request): if not already_authorized(request): response = self.wrap_json_response( {}, utils.response.ReturnCode.UNAUTHORIZED) else: data = [] open_id = request.session['open_id'] print(open_id) user = User.objects.get(open_id=open_id) cities = json.loads(user.focus_cities) for city in cities: result = juhe.weather(city['city'], city['area']) data.append(result) response = self.wrap_json_response(data=data) return JsonResponse(response, safe=False)
def post(self, request): recived_body = request.body recived_body = json.loads(recived_body) cities = recived_body.get('cities') data = [] # print(cities) for city in cities: # print(city) # print(city['city']) result = juhe.weather(city['city']) # print(result) result['city_info'] = city data.append(result) response_data = self.wrap_json_response(data=data) return JsonResponse(data=response_data, safe=False)
def post(self, request): received_body = request.body.decode('utf-8') received_body = json.loads(received_body) print('Received_body is:', received_body) cities = received_body.get('cities') print('Cities is: ', cities) data = [] for key in cities: item = key.get('area') item = item[:-1] print('Return city is:', item) response = juhe.weather(item) print('Return response is: ', response) response['province'] = key.get('province') response['city'] = key.get('city') response['area'] = key.get('area') data.append(response) data = self.wrap_json_response(data=data) print('Return data is: ', data) return JsonResponse(data=data, safe=False)
def get(self, request): if not already_authorized(request): response = self.wrap_json_response(data={}, code=ReturnCode.UNAUTHORIZED) else: open_id = request.session.get('open_id') user = User.objects.filter(open_id=open_id)[0] cities = json.loads(user.focus_cities) response = [] for key in cities: item = key.get('area') item = item[:-1] data = juhe.weather(item) data['province'] = key.get('province') data['city'] = key.get('city') data['area'] = key.get('area') response.append(data) data = self.wrap_json_response(data=response) print('Return get response is:', data) return JsonResponse(data=data, safe=False)
def get(self, request): """ 登录状态的关心城市天气显示 :param request: :return: """ if not already_authorized(request): response = self.wrap_json_response(code=ReturnCode.FAILED) return JsonResponse(data=response, safe=False) open_id = request.session.get('openid') user = User.objects.get(open_id=open_id) cities = json.loads(user.focus_cities) result = [] for city in cities: cityname = city.get('city') data_city = juhe.weather(cityname) data_city['city'] = cityname result.append(data_city) response = self.wrap_json_response(data=result, code=ReturnCode.SUCCESS) return JsonResponse(data=response, safe=False)