Checkbox remove is only removing 1 item

I am trying to create a template and view that will delete all checked items. I am fairly new and cant seem to figure out to get remove all items checked.

VIEWS.py

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse

from .models import Flight, Passenger
# Create your views here.


def index(request):

    context = {
        'flights': Flight.objects.all(),
    }
    return render(request, "flights/index.html", context=context)


def flight(request, flight_id):
    flight = Flight.objects.get(pk=flight_id)
    passengers = flight.passengers.all()
    non_passengers = Passenger.objects.exclude(flights=flight).all()

    context = {
        'flight': flight,
        'passengers': passengers,
        'non_passengers': non_passengers
    }

    return render(request, "flights/flight.html", context=context)


def book(request, flight_id):
    if request.method == "POST":
        flight = Flight.objects.get(pk=flight_id)
        passenger = Passenger.objects.get(pk=int(request.POST["passenger"]))
        passenger.flights.add(flight)
    return HttpResponseRedirect(reverse("flight", args=(flight.id,)))


def delete_booking(request, flight_id):
    if request.method == "POST":
        flight = Flight.objects.get(pk=flight_id)
        passenger = Passenger.objects.get(pk=int(request.POST["passenger"]))
        passenger.flights.remove(flight)
    return HttpResponseRedirect(reverse("flight", args=(flight.id,)))

FLIGHT.html

{% extends "base/base.html" %}

{% block body %}
    <h1>Flight {{ flight.id }}</h1>
    <ul>
        <li>Origin: {{ flight.origin }}</li>
        <li>Destination: {{ flight.destination }}</li>
        <li>Duration: {{ flight.duration}} </li>
    </ul>

    <h2>Passengers</h2>
    <form action="{% url 'delete_booking' flight.id %}" method="POST">
        {% csrf_token %}
    {% for passenger in passengers  %}
        <li><input type="checkbox" name="passenger" value="{{ passenger.id }}">{{ passenger }}</input></li>
    {% empty %}
        <li>No Passengers</li>
    {% endfor %}
    <button type="submit">Remove</button>
    </form>

    <h2>Add Passenger type 1</h2>
    <form action="{% url 'book' flight.id %}" method="POST">
        {% csrf_token %}
        <select name="passenger">
            {% for passenger in non_passengers  %}
                <option value="{{ passenger.id }}">{{ passenger }}</option>
            {% endfor %}
        </select>
        <input type="submit">
    </form>
    
    <a href="{% url 'index' %}">Back</a>
{% endblock body %}```

Use print to see what you are getting in request.POST["passenger"].

Also this line below will give you only one object i.e only one instance will be deleted
passenger = Passenger.objects.get(pk=int(request.POST["passenger"]))

Would it be .all() to gather all of the selections?

When printing request.POST["passenger"] it just returns one ID. Not sure how to get all selected.

all() will not give you all the values related to passenger.
also instead of this request.POST["passenger"] use request.POST.getlist("passenger") it will give you all the values (i.e id) of passenger in a list.

Here is the updated code however, I am getting the following error and items are not being removed.

def delete_booking(request, flight_id):
    if request.method == "POST":
        flight = Flight.objects.get(pk=flight_id)
        # passenger = Passenger.objects.get(
        #     pk=int(request.POST["remove_passenger"]))
        passenger = Passenger.objects.get(
            pk=int(request.POST.getlist(["remove_passenger"],)))
        print(request.POST.getlist("remove_passenger"))
        passenger.flights.remove(flight)
        print(passenger)
    return HttpResponseRedirect(reverse("flight", args=(flight.id,)))

If I don’t add “.getlist([“remove_passenger”])” to my pk=int line my print statement when print out a list of ids selected when I hit remove.

Here is the error I am getting:

TypeError at /flights/1/delete_booking/
unhashable type: 'list'
Request Method:	POST
Request URL:	http://127.0.0.1:8000/flights/1/delete_booking/

Its referring to this line.

pk=int(request.POST.getlist(["remove_passenger"],)))

I have tried it when the comma not being there, inside the trailing square bracket, right outside of the square bracket.

First thing understand that objects.get will only give you single object and when you are using this pk=remove_passenger_id than it should have to be single value not a list. By using this request.POST.getlist(["remove_passenger"] you are getting a list of ids.

In order to filter your pk from Passenger model, you can use something like this

remove_passenger_list = request.POST.getlist(["remove_passenger"])
remove_passenger_list = list(map(int, remove_passenger_list))

passenger = Passenger.objects.filter(pk__in=remove_passenger_list)