Home > Back-end >  Error when submitting flask-wtf into database
Error when submitting flask-wtf into database

Time:12-28

I am quite new to flask and I ran into a dead-end. I try to make a reservation form and save it into my database, but something is not working well.

Here is the form:

class ReservationForm(FlaskForm):
    checkIn = DateField('Start Date', format='%Y-%m-%d', validators=(validators.DataRequired(),))    
    checkOut = DateField('End Date', format='%Y-%m-%d', validators=(validators.DataRequired(),))
    guestNumber = IntegerField('Number of guests', validators=(validators.InputRequired(),))
    numberOfRooms = IntegerField('Number of rooms', validators=(validators.InputRequired(),))
    room_type = SelectField(choices=[(r, r) for r in Room.room_type.property.columns[0].type.enums])
    amount = FloatField('Total', validators=(validators.InputRequired(),))
    paymentType = SelectField(choices=[(p, p) for p in Payment.paymentType.property.columns[0].type.enums])

This is the route:

@auth.route('/reservation', methods=['GET', 'POST'])
def reservation():
    form = ReservationForm()

    if request.method == 'POST':
        checkIn = form.checkIn
        checkOut = form.checkOut
        guestNumber = form.guestNumber
        amount = form.amount
        
        new_reservation = Reservation(checkIn=checkIn, checkOut=checkOut, guestNumber=guestNumber, amount=amount, status = "New")
        db.session.add(new_reservation)
        db.session.commit()
        flash('Reservation created!', category='success')
        return redirect(url_for('/'))

    return render_template("reservation.html", form=form, user=current_user)

This is the database model:

class Reservation(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    guestNumber = db.Column(db.Integer, nullable=False)
    checkIn = db.Column(db.Date)
    checkOut = db.Column(db.Date)
    amount = db.Column(db.Integer, nullable=False)
    status = db.Column(db.Enum('New', 'Canceled', 'Passed', name='ReservationStatus'), nullable=False)

And finally the HTML code:

{% extends "base.html" %}
{% block title %} Reservation {% endblock %}

{% block content %}
<form method="POST">
  <h3 align="center">Make your reservation</h3>
  {{ form.hidden_tag() }}
    <div >
    <label for="checkIn">Check-in Date</label>
      {{form.checkIn(class_='form-control', type='date')}}
    </div>
    <div >
      <label for="checkOut">Check-out Date</label>
      {{form.checkOut(class_='form-control')}}
    </div>
    <div >
      <label for="numberOfRooms">Number of Rooms</label>
      {{form.numberOfRooms(class_='form-control')}}
  </div>
  <div >
    <label for="guestNumber">Number of Guests</label>
    {{form.guestNumber(class_='form-control')}}
  </div>
    <div >
      <label for="amount">Total</label>
      {{form.amount(class_='form-control')}}
    </div>

  <br />
  <button type="submit" >Submit</button>

</form>
{% endblock %}

And this is the complete traceback of the error I get on submit:

Error on request:
Traceback (most recent call last):
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 1719, in _execute_context        
    context = constructor(
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\default.py", line 1070, in _init_compiled       
    param = [
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\default.py", line 1071, in <listcomp>
    processors[key](compiled_params[key])
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\dialects\sqlite\base.py", line 1068, in process        
    raise TypeError(
TypeError: SQLite Date type only accepts Python date objects as input.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\werkzeug\serving.py", line 319, in run_wsgi
    execute(self.server.app)
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\werkzeug\serving.py", line 306, in execute
    application_iter = app(environ, start_response)
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 2095, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 2080, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Users\mcraciun\Documents\Maria\Disertatie\WebAppForHotelManagement\website\auth.py", line 111, in reservation
    db.session.commit()
  File "<string>", line 2, in commit
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\session.py", line 1431, in commit
    self._transaction.commit(_to_root=self.future)
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\session.py", line 829, in commit
    self._prepare_impl()
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\session.py", line 808, in _prepare_impl
    self.session.flush()
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\session.py", line 3363, in flush
    self._flush(objects)
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\session.py", line 3502, in _flush
    with util.safe_reraise():
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
    compat.raise_(
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
    raise exception
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\session.py", line 3463, in _flush
    flush_context.execute()
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 456, in execute
    rec.execute(self)
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 630, in execute
    util.preloaded.orm_persistence.save_obj(
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\persistence.py", line 245, in save_obj
    _emit_insert_statements(
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\persistence.py", line 1238, in _emit_insert_statements
    result = connection._execute_20(
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 1631, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\sql\elements.py", line 326, in _execute_on_connection  
    return connection._execute_clauseelement(
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 1498, in _execute_clauseelement  
    ret = self._execute_context(
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 1725, in _execute_context        
    self._handle_dbapi_exception(
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 2043, in _handle_dbapi_exception 
    util.raise_(
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
    raise exception
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 1719, in _execute_context        
    context = constructor(
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\default.py", line 1070, in _init_compiled       
    param = [
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\default.py", line 1071, in <listcomp>
    processors[key](compiled_params[key])
  File "C:\Users\mcraciun\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\dialects\sqlite\base.py", line 1068, in process        
    raise TypeError(
sqlalchemy.exc.StatementError: (builtins.TypeError) SQLite Date type only accepts Python date objects as input.
[SQL: INSERT INTO reservation ("guestNumber", "checkIn", "checkOut", amount, status, hotel_id) VALUES (?, ?, ?, ?, ?, ?)]
[parameters: [{'status': 'New', 'amount': <wtforms.fields.numeric.FloatField object at 0x0000029F0B3BD510>, 'checkIn': <wtforms.fields.datetime.DateField object at ... (51 characters truncated) ... s.numeric.IntegerField object at 0x0000029F0B3BD2D0>, 'checkOut': <wtforms.fields.datetime.DateField object at 0x0000029F0B3BD210>, 'hotel_id': None}]]

Any help is much appreciated :)

CodePudding user response:

From your traceback:

TypeError: SQLite Date type only accepts Python date objects as input.

Therefore we can make a conclusion that you are sending a wrong data type to the database, try using datetime object in yoru view instead like:

from datetime import datetime

... your code ...

if request.method == 'POST':
   checkIn = datetime.datetime.strptime(
             request.form['checkOut'],
             '%Y-%m-%d')
   checkOut = datetime.datetime.strptime(
              request.form['checkOut'],
              '%Y-%m-%d')

... your code continues ...   

Then it should work.

CodePudding user response:

Thanks to @SLDem's answer I was able to fix the error I was getting on the date fields, but I also noted that I was getting the values from the form wrongly.

Made this modification in the route and now the code works:

@auth.route('/reservation', methods=['GET', 'POST'])
def reservation():
    form = ReservationForm()

    if request.method == 'POST':
        checkIn = datetime.strptime(
             request.form['checkIn'], '%Y-%m-%d')
        checkOut = datetime.strptime(
             request.form['checkOut'], '%Y-%m-%d')
        guestNumber = request.form['guestNumber']
        amount = request.form['amount']
  • Related