0

I'm pretty new to Flask and struggling with transferring data from a form to an SQL Database. The form is within a modal, which works perfectly and will redirect back to the main page once the form is submitted. However, the data from the form is not transferred to the SQL Database that I have associated with this project. I'm sure I'm missing something quite simple, help would be appreciated! If any more code screenshots are needed please let me know.

HTML Template for registration modal

<div class="modal fade" id="registerModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="registerModalLabel">Register</h4>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form method="POST" action="" novalidate>
                    {{ registration_form.hidden_tag() }}
                    <div class="mb-2">
                        {{ registration_form.employee_id.label(class="form-label") }}
                        {{ registration_form.employee_id(class="form-control") }}
                        {% for error in registration_form.employee_id.errors %}
                            <span style="color: red;">[{{ error }}]</span>
                    </div>
                    <div class="mb-2">
                        {{ registration_form.email.label(class="form-label") }}
                        {{ registration_form.email(class="form-control") }}
                        {% for error in registration_form.email.errors %}
                            <span style="color: red;">[{{ error }}]</span>
                    </div>
                    <div class="mb-2">
                        {{ registration_form.password.label(class="form-label") }}
                        {{ registration_form.password(class="form-control") }}
                        {% for error in registration_form.password.errors %}
                            <span style="color: red;">[{{ error }}]</span>
                    </div>
                    <div class="mb-2">
                        {{ registration_form.confirm_password.label(class="form-label") }}
                        {{ registration_form.confirm_password(class="form-control") }}
                        {% for error in registration_form.confirm_password.errors %}
                            <span style="color: red;">[{{ error }}]</span>
                    </div>
                    <div class="mb-2">
                        {{ registration_form.submit(class="btn btn-primary") }}
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

Index routing function where the form data is sent to

@app.route('/index', methods=['GET', 'POST'])
def index():
    if current_user.is_authenticated:
        return redirect(url_for('index'))

    registration_form = RegistrationForm()
    login_form = LoginForm()

    if registration_form.validate_on_submit():

        employee_id = registration_form.employee_id.data
        email = registration_form.email.data
        password = registration_form.password.data

        # Check if employee ID or email already exists
        existing_employee_id = Employee.query.filter_by(employee_id=employee_id).first()
        existing_email = Employee.query.filter_by(email=email).first()

        if existing_employee_id:
            flash('This ID is already in use.', 'danger')
        elif existing_email:
            flash('This email is already registered.', 'danger')
        else:

            new_employee = Employee(
                employee_id=employee_id,
                email=email,
                password_hash=generate_password_hash(password, salt_length=32)
            )
            print(new_employee)
            db.session.add(new_employee)
            try:
                db.session.commit()
                flash(f'Registration successful for employee {employee_id}.', 'success')
                return redirect(url_for('index'))
            except:
                db.session.rollback()
                flash(f'Failed to register user', 'danger')

    return render_template('index.html', registration_form=registration_form, login_form=login_form)

I wanted the data from the modal form to commit to my SQL database. The form completes and closes, yet the data is not transferred.

0

Browse other questions tagged or ask your own question.