Change Form Redirect URL depending on selection

61 views
Skip to first unread message

Silvian Cedru

unread,
Aug 25, 2022, 5:48:05 AM8/25/22
to web2py-users
I have a form with a few selections but I want to redirect it to a other page depends on the selection does someone know how to do that ?

Jim S

unread,
Aug 25, 2022, 3:30:56 PM8/25/22
to web2py-users
Can you share the code you have so far?

I'd probably do something like:

form = SQLFORM.factory(...your form definition here...)

if form.process().accepted:
    if form.vars.field_1 == 'some test case you want to redirect on':
        redirect(URL('my_redirect_url', vars=dict(my vars to pass to the redirect))
    elif form.vars.field_2 == 'another test case you want to redirect on':
        redirect(URL('my other redirect url')

-Jim

Silvian Cedru

unread,
Aug 26, 2022, 3:37:51 AM8/26/22
to web2py-users
Sure thanks in advance that is my DB so for example if I select company I want it to redirect to company and if I select a certrain country lets say germany or so I want it to redirect to the german page of company would that be possible ?

db.define_table('partner',
Field('name'),
Field('address'),
Field('country',requires=IS_IN_SET(countries)),
Field('postalcode'),
Field('entity_type',requires=IS_IN_SET(['Company','Person'])),
Field('partner_manager', 'reference auth_user', requires = IS_IN_DB(db, 'auth_user.id','%(last_name)s')),
Field('email'),
Field('web'))

that is my controller:

def partner():
    form = SQLFORM(db.partner, _name='form_partner')
    if form.process().accepted:
        session.flash = 'Partner added !'
        redirect(URL('partnerlist'))
    else:
        session.flash = 'Please try again !'
    return dict(form=form)

Jim S

unread,
Aug 26, 2022, 12:36:30 PM8/26/22
to web2py-users
It is doing to depend on how your URLs are structured for each country page.  I'm guessing you'll have a different endpoint for each country in your set of countries.

Likewise for Companies.  You said 'if I select company I want to redirect to company'.  Based on the code you posted I'm not sure what that means.  You don't have a 'company' field, just an entity type called country.  What is the endpoint that you want it to redirect to?

-Jim

Silvian Cedru

unread,
Aug 29, 2022, 2:50:27 AM8/29/22
to web2py-users
Actually I have 3 endpoints tier 1 tier 2 and tier 3 so example if choosing Germany it should redirect to www.website/tier1.com if choosing for example Viertnam it should redirect to www.website/tier3.com that is what I try to achieve but one thing is I have all my countries in arrays sorted from tier 1 to tier 3. 

Dave S

unread,
Aug 29, 2022, 8:27:25 AM8/29/22
to web2py-users
On Sunday, August 28, 2022 at 7:50:27 PM UTC-7 silvia...@gmail.com wrote:
Actually I have 3 endpoints tier 1 tier 2 and tier 3 so example if choosing Germany it should redirect to www.website/tier1.com if choosing for example Viertnam it should redirect to www.website/tier3.com that is what I try to achieve but one thing is I have all my countries in arrays sorted from tier 1 to tier 3. 


If those arrays are globally defined (as in db.py), then you just do 
       if country in tier1_array:  redirect(URL("tier1.com", ...))"
which is pretty much the same as Jim's suggestion.  If you need to look into the database which tier the country is in,  you probably would look that up using one of the shortcuts shown in the DAL chapter of the book.

/dps
Message has been deleted
Message has been deleted
Message has been deleted

Dave S

unread,
Sep 3, 2022, 8:36:30 AM9/3/22
to web2py-users


On Monday, August 29, 2022 at 3:20:22 AM UTC-7 silvia...@gmail.com wrote:
That would be my controller function I know it is totally wrong but dont know what tu put in there to be honest 


def add():
    form = SQLFORM(db.projects, _name='form_add')
   
    if country in (countryt1)
    redirect(URL('view', vars=dict(countryt1=countryt1))  
    return locals()

Well, maybe if you can mock up some screen shots showing  what you want the user to see, we can provide less vague advice.  
You may be wanting some extra buttons in your form besides the submit button.

BTW, if you're wanting the "country" variable to come from "form_add", you need to say something like
     if form_add.vars.country in  thesetofcountries:

If you're looking up the country in a database table, something like
    countrydata = db.countryt1[db.countryt1.countryname == form_add.vars.country]
  if countrydata:

and the paragrapsh following that.

/dps
Reply all
Reply to author
Forward
0 new messages