I am deploying a SelectMultipleField in order for the user to select containers to move to a different booking.
The problem is that everytime I submit the button, I go to the index page and check the DB, and the records are unchanged.
I have zero idea what could be...
routes.py
# route for container transfer from one booking to another booking
@blueprint.route('/bookings/<booking_number>/transfer', methods=['GET', 'POST'])
@login_required
def transfer(booking_number):
form = TransferForm()
# get data for the forms
form.containers.choices = [(str(container.id), str(container.container_number), str(container.container_type), str(container.container_size), str(container.CadastroBooking.booking_number)) for container in ContainerRegister.query.all()]
form.booking.choices = [(str(booking.id), str(booking.booking_number)) for booking in CadastroBooking.query.all()]
# get the data from containers form
containers = request.form.getlist('containers')
if request.method == "POST" and form.validate_on_submit():
# loop through the containers and update the booking number
for container in containers:
new_booking = request.form['booking']
transfer_container = ContainerRegister.query.filter_by(id=container).first_or_404()
booking_transf = BookingRegister.query.filter_by(id=new_booking).first_or_404()
transfer_container.booking_id = new_booking.id
db.session.commit()
return redirect(url_for('home.index'))
return render_template('transfer.html', form=form, booking=booking, containers=containers)
forms.py
class TransferForm(FlaskForm):
containers = SelectMultipleField('Containers - Booking:', validators=[DataRequired()], choices=[], render_kw={"placeholder": "Select containers"})
booking = SelectField(' New Booking for Transfer:', validators=[DataRequired()], choices=[], render_kw={"placeholder": "Select Booking"})
)
submit = SubmitField("Transfer")
models.py
class ContainerRegister(db.Model):
__tablename__ = 'ContainerRegister'
id = Column(Integer, primary_key=True)
booking_id = Column(Integer, db.ForeignKey('BookingRegister.id'))
class BookingRegister(db.Model):
__tablename__ = 'BookingRegister'
id = Column(Integer, primary_key=True)
/templates/transfer.html
<form method="POST"> <!-- Create form to display booking data -->
<form action="{{ url_for('divlog.transfer', booking_number=booking.booking_number)}}" method="POST">
<div class="mb-4">
<!-- Choice Select -->
<div class="mb-2">
<label class="my-1 me-2" for="state">Container - Booking:</label>
<select id="containers" class="w-100 mb-0" name="container" multiple="multiple">
{% for choice in form.containers.choices %}
<option value="{{ choice[0] }}">
{{ choice[1] }} {{ choice[2] }} {{ choice[3] }} {{ choice[4] }}
</option>
{% endfor %}
</select>
</div>
<!-- Form -->
<div class="mb-4">
{{ form.booking.label(class="form-label") }}
{{ form.booking(class="form-control") }}
</div>
<!-- Form -->
<div class="mb-4">
{{ form.submit(class="btn btn-lg btn-primary") }}
</div>
</div>
</form>