def create(): # Create a new instance of our CarForm class using the request.form # object which will get the request method (GET or POST) as the first # parameter form = CarForm(request.form, csrf_enabled=False) # To check if our require fields have been filled out and then create a new # instance of our Car class and its contructor method will take the form # object if form.validate_on_submit(): car = Car(form) # Since, our object has been created; use an INSERT INTO statement to # add the object's properties to our database query = "INSERT INTO `cars` (`name`, `image`, `topspeed`) VALUES (%s, %s, %s)" # query = "INSERT INTO `cars` (`name`, `image`, `topspeed`) VALUES (%s, %s, %d)" value = (car.name, car.image, car.topspeed) mycursor.execute(query, value) mydb.commit() # If the required fields have been filled out add the car to the # database and redirect to the View Cars page with GET data to # display a success message, else go back to the Add Car page with # GET data to display an error message return redirect('/?add=success') else: return redirect('/add-car?add=error')
def home(): car_form = CarForm() if car_form.validate_on_submit(): car_age = int(car_form.car_age.data) car_fuel = car_form.car_fuel.data car_doors = int(car_form.car_doors.data) car_cc = int(car_form.car_cc.data) car_horsepower = int(car_form.car_horsepower.data) car_transmission = car_form.car_transmission.data car_odometer = int(car_form.car_odometer.data) car_weight = car_form.car_weight.data car_color = car_form.car_color.data return render_template("car.html", form=car_form)