1

I am working on building a flight price search website as part of a project and have managed to get most of the functionality I require working.

Here's what the front-end looks like so far:

When users enter in the details, a POST request is sent to an API.

API: https://developers.amadeus.com/self-service/category/air/api-doc/flight-offers-search

API Python SDK: https://github.com/amadeus4dev/amadeus-python

I'm receiving a response from the API; however, am struggling to understand what I should then do with the information received. The next step is to pass the information from the API into a search results template and split out all of the received flight information, similar to how it is displayed on Sky Scanner:

Below is what I have in my views.py so far (currently passing in the whole response, in what I believe is JSON format?)

Views.py:

def flight_search(request): 
    kwargs = {'originLocationCode': request.POST.get('Origincity'), 
            'destinationLocationCode': request.POST.get('Destinationcity'), 
            'departureDate': request.POST.get('Departuredate'), 
            'returnDate': request.POST.get('Returndate'),
            'adults': '1'}
            
    try: 
        response = amadeus.shopping.flight_offers_search.get(
        **kwargs)
        print(response.data)   
        return render(request, "flightfinder/flight_search.html", {
            "response": response
    })
    except ResponseError as error:
        raise error

Here is an example response from the API:

{
    "meta": {
        "count": 2
    },
    "data": [
        {
            "type": "flight-offer",
            "id": "1",
            "source": "GDS",
            "instantTicketingRequired": false,
            "nonHomogeneous": false,
            "oneWay": false,
            "lastTicketingDate": "2020-11-20",
            "numberOfBookableSeats": 2,
            "itineraries": [
                {
                    "duration": "PT22H40M",
                    "segments": [
                        {
                            "departure": {
                                "iataCode": "GIG",
                                "terminal": "2",
                                "at": "2020-12-01T16:30:00"
                            },
                            "arrival": {
                                "iataCode": "CDG",
                                "terminal": "2E",
                                "at": "2020-12-02T07:45:00"
                            },
                            "carrierCode": "AF",
                            "number": "443",
                            "aircraft": {
                                "code": "77W"
                            },
                            "operating": {
                                "carrierCode": "AF"
                            },
                            "duration": "PT11H15M",
                            "id": "3",
                            "numberOfStops": 0,
                            "blacklistedInEU": false
                        },
                        {
                            "departure": {
                                "iataCode": "ORY",
                                "terminal": "2",
                                "at": "2020-12-02T17:10:00"
                            },
                            "arrival": {
                                "iataCode": "MAD",
                                "terminal": "2",
                                "at": "2020-12-02T19:10:00"
                            },
                            "carrierCode": "AF",
                            "number": "4792",
                            "aircraft": {
                                "code": "73H"
                            },
                            "operating": {
                                "carrierCode": "UX"
                            },
                            "duration": "PT2H",
                            "id": "4",
                            "numberOfStops": 0,
                            "blacklistedInEU": false
                        }
                    ]
                },
                {
                    "duration": "PT15H5M",
                    "segments": [
                        {
                            "departure": {
                                "iataCode": "MAD",
                                "terminal": "2",
                                "at": "2020-12-12T20:05:00"
                            },
                            "arrival": {
                                "iataCode": "CDG",
                                "terminal": "2F",
                                "at": "2020-12-12T22:20:00"
                            },
                            "carrierCode": "AF",
                            "number": "1101",
                            "aircraft": {
                                "code": "319"
                            },
                            "operating": {
                                "carrierCode": "AF"
                            },
                            "duration": "PT2H15M",
                            "id": "5",
                            "numberOfStops": 0,
                            "blacklistedInEU": false
                        },
                        {
                            "departure": {
                                "iataCode": "CDG",
                                "terminal": "2E",
                                "at": "2020-12-12T23:35:00"
                            },
                            "arrival": {
                                "iataCode": "GIG",
                                "terminal": "2",
                                "at": "2020-12-13T07:10:00"
                            },
                            "carrierCode": "AF",
                            "number": "442",
                            "aircraft": {
                                "code": "77W"
                            },
                            "operating": {
                                "carrierCode": "AF"
                            },
                            "duration": "PT11H35M",
                            "id": "6",
                            "numberOfStops": 0,
                            "blacklistedInEU": false
                        }
                    ]
                }
            ],
            "price": {
                "currency": "USD",
                "total": "2324.04",
                "base": "2089.00",
                "fees": [
                    {
                        "amount": "0.00",
                        "type": "SUPPLIER"
                    },
                    {
                        "amount": "0.00",
                        "type": "TICKETING"
                    }
                ],
                "grandTotal": "2324.04"
            },
            "pricingOptions": {
                "fareType": [
                    "PUBLISHED"
                ],
                "includedCheckedBagsOnly": false
            },
            "validatingAirlineCodes": [
                "AF"
            ],
            "travelerPricings": [
                {
                    "travelerId": "1",
                    "fareOption": "STANDARD",
                    "travelerType": "ADULT",
                    "price": {
                        "currency": "USD",
                        "total": "1311.52",
                        "base": "1194.00"
                    },
                    "fareDetailsBySegment": [
                        {
                            "segmentId": "3",
                            "cabin": "BUSINESS",
                            "fareBasis": "OS52OOND",
                            "brandedFare": "BUSINESS",
                            "class": "O",
                            "includedCheckedBags": {
                                "quantity": 2
                            }
                        },
                        {
                            "segmentId": "4",
                            "cabin": "BUSINESS",
                            "fareBasis": "OS52OOND",
                            "brandedFare": "BUSINESS",
                            "class": "J",
                            "includedCheckedBags": {
                                "quantity": 2
                            }
                        },
                        {
                            "segmentId": "5",
                            "cabin": "ECONOMY",
                            "fareBasis": "RL50TCLD",
                            "brandedFare": "LIGHT3",
                            "class": "L",
                            "includedCheckedBags": {
                                "quantity": 0
                            }
                        },
                        {
                            "segmentId": "6",
                            "cabin": "ECONOMY",
                            "fareBasis": "RL50TCLD",
                            "brandedFare": "LIGHT3",
                            "class": "R",
                            "includedCheckedBags": {
                                "quantity": 0
                            }
                        }
                    ]
                },
                {
                    "travelerId": "2",
                    "fareOption": "STANDARD",
                    "travelerType": "CHILD",
                    "price": {
                        "currency": "USD",
                        "total": "1012.52",
                        "base": "895.00"
                    },
                    "fareDetailsBySegment": [
                        {
                            "segmentId": "3",
                            "cabin": "BUSINESS",
                            "fareBasis": "OS52OOND",
                            "brandedFare": "BUSINESS",
                            "class": "O"
                        },
                        {
                            "segmentId": "4",
                            "cabin": "BUSINESS",
                            "fareBasis": "OS52OOND",
                            "brandedFare": "BUSINESS",
                            "class": "J"
                        },
                        {
                            "segmentId": "5",
                            "cabin": "ECONOMY",
                            "fareBasis": "RL50TCLD",
                            "brandedFare": "LIGHT3",
                            "class": "L"
                        },
                        {
                            "segmentId": "6",
                            "cabin": "ECONOMY",
                            "fareBasis": "RL50TCLD",
                            "brandedFare": "LIGHT3",
                            "class": "R"
                        }
                    ]
                }
            ]
        },
        {
            "type": "flight-offer",
            "id": "2",
            "source": "GDS",
            "instantTicketingRequired": false,
            "nonHomogeneous": false,
            "oneWay": false,
            "lastTicketingDate": "2020-11-20",
            "numberOfBookableSeats": 2,
            "itineraries": [
                {
                    "duration": "PT15H10M",
                    "segments": [
                        {
                            "departure": {
                                "iataCode": "GIG",
                                "terminal": "2",
                                "at": "2020-12-01T16:30:00"
                            },
                            "arrival": {
                                "iataCode": "CDG",
                                "terminal": "2E",
                                "at": "2020-12-02T07:45:00"
                            },
                            "carrierCode": "AF",
                            "number": "443",
                            "aircraft": {
                                "code": "77W"
                            },
                            "operating": {
                                "carrierCode": "AF"
                            },
                            "duration": "PT11H15M",
                            "id": "1",
                            "numberOfStops": 0,
                            "blacklistedInEU": false
                        },
                        {
                            "departure": {
                                "iataCode": "CDG",
                                "terminal": "2F",
                                "at": "2020-12-02T09:35:00"
                            },
                            "arrival": {
                                "iataCode": "MAD",
                                "terminal": "2",
                                "at": "2020-12-02T11:40:00"
                            },
                            "carrierCode": "AF",
                            "number": "1300",
                            "aircraft": {
                                "code": "319"
                            },
                            "operating": {
                                "carrierCode": "AF"
                            },
                            "duration": "PT2H5M",
                            "id": "2",
                            "numberOfStops": 0,
                            "blacklistedInEU": false
                        }
                    ]
                },
                {
                    "duration": "PT15H5M",
                    "segments": [
                        {
                            "departure": {
                                "iataCode": "MAD",
                                "terminal": "2",
                                "at": "2020-12-12T20:05:00"
                            },
                            "arrival": {
                                "iataCode": "CDG",
                                "terminal": "2F",
                                "at": "2020-12-12T22:20:00"
                            },
                            "carrierCode": "AF",
                            "number": "1101",
                            "aircraft": {
                                "code": "319"
                            },
                            "operating": {
                                "carrierCode": "AF"
                            },
                            "duration": "PT2H15M",
                            "id": "5",
                            "numberOfStops": 0,
                            "blacklistedInEU": false
                        },
                        {
                            "departure": {
                                "iataCode": "CDG",
                                "terminal": "2E",
                                "at": "2020-12-12T23:35:00"
                            },
                            "arrival": {
                                "iataCode": "GIG",
                                "terminal": "2",
                                "at": "2020-12-13T07:10:00"
                            },
                            "carrierCode": "AF",
                            "number": "442",
                            "aircraft": {
                                "code": "77W"
                            },
                            "operating": {
                                "carrierCode": "AF"
                            },
                            "duration": "PT11H35M",
                            "id": "6",
                            "numberOfStops": 0,
                            "blacklistedInEU": false
                        }
                    ]
                }
            ],
            "price": {
                "currency": "USD",
                "total": "2334.44",
                "base": "2089.00",
                "fees": [
                    {
                        "amount": "0.00",
                        "type": "SUPPLIER"
                    },
                    {
                        "amount": "0.00",
                        "type": "TICKETING"
                    }
                ],
                "grandTotal": "2334.44"
            },
            "pricingOptions": {
                "fareType": [
                    "PUBLISHED"
                ],
                "includedCheckedBagsOnly": false
            },
            "validatingAirlineCodes": [
                "AF"
            ],
            "travelerPricings": [
                {
                    "travelerId": "1",
                    "fareOption": "STANDARD",
                    "travelerType": "ADULT",
                    "price": {
                        "currency": "USD",
                        "total": "1316.72",
                        "base": "1194.00"
                    },
                    "fareDetailsBySegment": [
                        {
                            "segmentId": "1",
                            "cabin": "BUSINESS",
                            "fareBasis": "OS52OOND",
                            "brandedFare": "BUSINESS",
                            "class": "O",
                            "includedCheckedBags": {
                                "quantity": 2
                            }
                        },
                        {
                            "segmentId": "2",
                            "cabin": "BUSINESS",
                            "fareBasis": "OS52OOND",
                            "brandedFare": "BUSINESS",
                            "class": "J",
                            "includedCheckedBags": {
                                "quantity": 2
                            }
                        },
                        {
                            "segmentId": "5",
                            "cabin": "ECONOMY",
                            "fareBasis": "RL50TCLD",
                            "brandedFare": "LIGHT3",
                            "class": "L",
                            "includedCheckedBags": {
                                "quantity": 0
                            }
                        },
                        {
                            "segmentId": "6",
                            "cabin": "ECONOMY",
                            "fareBasis": "RL50TCLD",
                            "brandedFare": "LIGHT3",
                            "class": "R",
                            "includedCheckedBags": {
                                "quantity": 0
                            }
                        }
                    ]
                },
                {
                    "travelerId": "2",
                    "fareOption": "STANDARD",
                    "travelerType": "CHILD",
                    "price": {
                        "currency": "USD",
                        "total": "1017.72",
                        "base": "895.00"
                    },
                    "fareDetailsBySegment": [
                        {
                            "segmentId": "1",
                            "cabin": "BUSINESS",
                            "fareBasis": "OS52OOND",
                            "brandedFare": "BUSINESS",
                            "class": "O"
                        },
                        {
                            "segmentId": "2",
                            "cabin": "BUSINESS",
                            "fareBasis": "OS52OOND",
                            "brandedFare": "BUSINESS",
                            "class": "J"
                        },
                        {
                            "segmentId": "5",
                            "cabin": "ECONOMY",
                            "fareBasis": "RL50TCLD",
                            "brandedFare": "LIGHT3",
                            "class": "L"
                        },
                        {
                            "segmentId": "6",
                            "cabin": "ECONOMY",
                            "fareBasis": "RL50TCLD",
                            "brandedFare": "LIGHT3",
                            "class": "R"
                        }
                    ]
                }
            ]
        }
    ],
    "dictionaries": {
        "locations": {
            "MAD": {
                "cityCode": "MAD",
                "countryCode": "ES"
            },
            "GIG": {
                "cityCode": "RIO",
                "countryCode": "BR"
            },
            "CDG": {
                "cityCode": "PAR",
                "countryCode": "FR"
            },
            "ORY": {
                "cityCode": "PAR",
                "countryCode": "FR"
            }
        },
        "aircraft": {
            "77W": "BOEING 777-300ER",
            "319": "AIRBUS A319",
            "73H": "BOEING 737-800 (WINGLETS)"
        },
        "currencies": {
            "USD": "US DOLLAR"
        },
        "carriers": {
            "UX": "AIR EUROPA",
            "AF": "AIR FRANCE"
        }
    }
}

My question is: how should I pass this information to the search results template so that I can populate the page with all of the flight information?

Should I pull out the information I need into lists and then pass these through to the template which displays the flight search results? (I'm not sure how I would then ensure the information pertained to a particular flight on the search results template)

Or should I simply pass the response and then extract the information from it on the template?

My apologies for the vague question here, please let me know if you require any additional information - I'm essentially stuck on how to handle this response and would really appreciate any guidance that anybody has.

Thank you!

Tjm92
  • 167
  • 2
  • 19
  • Does this answer your question? [Django: passing JSON from view to template](https://stackoverflow.com/questions/31151229/django-passing-json-from-view-to-template) – Muteshi Nov 13 '20 at 21:35
  • Feel free to check out this open-source project that could be useful for your use case: https://github.com/amadeus4dev/amadeus-flight-booking-django. You can see how the Flight class extracts the information per flight returned by the API and then in the views.py the list of flights is sent to the UI – anna_ts Dec 02 '20 at 09:41

0 Answers0