Extracting just submitted details in a redirect

18 views
Skip to first unread message

mostwanted

unread,
Aug 20, 2023, 7:04:51 AM8/20/23
to web2py-users
I have a student registration form that immediately after registering a student I want to use those details in another form, a payments form to enter that student's payment status but my code keeps returning  none like student is not available, I am doing something wrong here please assist me figure it out or show me a better way of doing achieving the same logic:

CODE:
def registration():
    form=SQLFORM(db.store_registration)
    if form.process().accepted:
        response.flash=T('Details Registered')
        student_name = "{form.vars.first_name} {form.vars.last_name}"
        # Redirect to payments view with student's name as parameter
        redirect(URL('default', 'payments', vars={'student_name': student_name}))
    return locals()

def payments():
    student_name = request.vars.student_name

    # Loading student's information from the database
    student = db(db.store_registration.first_name + ' ' + db.store_registration.last_name == student_name).select().first()
   
    if student is None:
        raise HTTP(404, "Student not found")
    form.vars.student = student.id  # Set the student reference in the payment form
    form = SQLFORM(db.payments)
    if form.process().accepted:
        response.flash = T('Payment details submitted')
    return locals()


Regards

Clemens

unread,
Aug 20, 2023, 9:25:43 AM8/20/23
to web2py-users
Hi,

could it be that the problem is, you're passing the students name (vars={'student_name': student_name}) but then reference to form.vars.student as student.id?

Regards

mostwanted

unread,
Aug 20, 2023, 3:14:37 PM8/20/23
to web2py-users
I managed to get it going, thanks for highlighting that mistake 

form=SQLFORM(db.store_registration)
    if form.process().accepted:
        response.flash=T('Details Registered')
        student_id = form.vars.id_number

        # Redirect to payments view with student's name as parameter
        redirect(URL('default', 'payments', vars={'student_id': student_id}))
    return locals()

def payments():
    student_id = request.vars.student_id
    # Load student's information from the database
    student = db(db.store_registration.id_number == student_id).select().first()

   
    if student is None:
        raise HTTP(404, "Student not found")

    db.payments.student.default=student.id
    form = SQLFORM(db.payments)
    #form.vars.student = student.id  # Set the student reference in the payment form


    if form.process().accepted:
        response.flash = T('Payment details submitted')
        redirect(URL('default', 'student_registration'))

    return locals()


Reply all
Reply to author
Forward
0 new messages