Python project wrongly linked to Django
I'm pretty new with Django and I'm making a Flight Searching App with the Amadeus API. I've made it so, that it creates a .csv file with the cheapest flights from one destination to another. I now want to create a web application that lets the user enter cities and dates and displays the .csv file.
I've made the HTML template but whenever I click on "Search" I get thrown a 404 Error. I think there is an error with urls.py or views.py but can't seem to figure it out.
This is the 404 Error: "Safari cannot open the page, localhost:63342/Flights/FlightsDirectory/FlightsDirectory/flightsapp/templates/%7B%%20url%20'buscar_vuelos'%20%%7D due to an unexpected connection closure by the server. This can sometimes happen when the server is overloaded. Please wait a few minutes and try again."
This is my views.py
from django.shortcuts import render
import requests
import pandas as pd
def search_flights(request):
if request.method == 'POST':
origen = request.POST.get('origen')
destino = request.POST.get('destino')
fecha_ida = request.POST.get('fecha_ida')
fecha_regreso = request.POST.get('fecha_regreso')
params = {
"originLocationCode": origen,
"destinationLocationCode": destino,
"departureDate": fecha_ida,
"returnDate": fecha_regreso,
"adults": 1,
"max": 7
}
url = "https://test.api.amadeus.com/v2/shopping/flight-offers"
access_token = "*********************"
headers = {
"Authorization": f"Bearer {access_token}"
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
resultados = response.json().get("data", [])
vuelos_data = []
for vuelo in resultados:
price = vuelo.get("price", {}).get("total", "N/A")
currency = vuelo.get("price", {}).get("currency", "N/A")
for itinerary in vuelo.get("itineraries", []):
for segment in itinerary.get("segments", []):
departure = segment.get("departure", {})
arrival = segment.get("arrival", {})
carrier_code = segment.get("carrierCode", "N/A")
duration = segment.get("duration", "N/A")
vuelos_data.append({
"Aerolínea": carrier_code,
"Precio": f"{price} {currency}",
"Duración": duration,
"Salida": departure.get("iataCode", "N/A"),
"Hora Salida": departure.get("at", "N/A").replace('T', ' '),
"Llegada": arrival.get("iataCode", "N/A"),
"Hora Llegada": arrival.get("at", "N/A").replace('T', ' ')
})
if vuelos_data:
df = pd.DataFrame(vuelos_data)
df.to_csv("vuelos.csv", index=False)
return render(request, 'base.html', {'vuelos': vuelos_data})
else:
return render(request, 'base.html', {'error': response.text})
return render(request, 'base.html')
This is the urls.py inside of the same folder where views.py is
from django.urls import path
from . import views
urlpatterns = [
path("", views.search_flights, name ="search_flights")
]
This is the HTML code
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Búsqueda de Vuelos</title>
</head>
<body>
<form method="post" action="{% url 'search_flights' %}">
{% csrf_token %}
<input type="text" name="origen" placeholder="Origen" required>
<input type="text" name="destino" placeholder="Destino" required>
<input type="date" name="fecha_ida" required>
<input type="date" name="fecha_regreso" required>
<button type="submit">Buscar</button>
</form>
<h2>Vuelos Encontrados</h2>
<table>
<thead>
<tr>
<th>Aerolinea</th>
<th>Precio</th>
<th>Duración</th>
<th>Salida</th>
<th>Hora Salida</th>
<th>Llegada</th>
<th>Hora Llegada</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ vuelo.Aerolínea }}</td>
<td>{{ vuelo.Precio }}</td>
<td>{{ vuelo.Duración }}</td>
<td>{{ vuelo.Salida }}</td>
<td>{{ vuelo.Hora Salida }}</td>
<td>{{ vuelo.Llegada }}</td>
<td>{{ vuelo.Hora Llegada }}</td>
</tr>
<tr>
<td colspan="7">No se encontraron vuelos</td>
</tr>
</tbody>
</table>
</body>
</html>