У меня есть эта функция, которая связывается с SWAPI API, и я хочу получить название вида для фильма.Допустим, я ищу «Новая надежда», я должен получить ['Hutt','Wookiee','Droid','Human','Rodian']
.Проблема в том, что в диктовке SWAPI вы не получаете имена, вы получаете список URL-адресов.
"species": [
"https://swapi.co/api/species/5/",
"https://swapi.co/api/species/3/",
"https://swapi.co/api/species/2/",
"https://swapi.co/api/species/1/",
"https://swapi.co/api/species/4/"
],
Пока это мое утверждение:
@api_view(['GET'])
def search_films(request,title):
context = {}
species = []
url = "https://swapi.co/api/?search=" + str(title)
if request.method == "GET":
r = requests.get(url)
if r.status_code == 200:
data = r.json()
species = data['results'][0]['species']
if species:
for species_url in species:
get_request = requests.get(species_url)
response_data = get_request.json()
species.append(response_data)
context['species'] = response_data
print(context)
return Response(data, status=status.HTTP_200_OK)
else:
return Response({"error": "Request failed"}, status=r.status_code)
else:
return Response({"error": "Method not allowed"}, status=status.HTTP_400_BAD_REQUEST)
ВВ этот момент я получаю сообщение об ошибке:
raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for '{'name': 'Hutt', 'classification': 'gastropod', 'designation': 'sentient', 'average_height': '300', 'skin_colors': 'green, brown, tan', 'hair_colors': 'n/a', 'eye_colors': 'yellow, red', 'average_lifespan': '1000', 'homeworld': 'https://swapi.co/api/planets/24/', 'language': 'Huttese', 'people': ['https://swapi.co/api/people/16/'], 'films': ['https://swapi.co/api/films/3/', 'https://swapi.co/api/films/1/'], 'created': '2014-12-10T17:12:50.410000Z', 'edited': '2014-12-20T21:36:42.146000Z', 'url': 'https://swapi.co/api/species/5/'}'
Есть идеи, как это реализовать?