It is not working because you need the list of options when the form is 
generated, not when it is submitted, in fact this line is executed when the 
form is generated:

db.auth_user.city.requires = 
IS_IN_SET(COUNTRIESANDCITIES[request.post_vars.country]) 
 

yet at this time the form is not submitted yet and request.post_vars.country 
is None.

You cannot do it this way. Conditional options require some JS programming. 
Something like this:
http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/
but with web2py not PHP.

On Thursday, 7 November 2013 10:39:16 UTC-6, Dr. Bill Y.K. Lim wrote:
>
> Hi, I am new to Python and web2py and I am not sure if my approach below 
> is correct - in any case I have a problem with it:
>
> I have added 2 extra fields in db.py as follows:
>
> from applications.myapp.modules.countriesandcities import *
> auth.settings.extra_fields['auth_user']=[
>    Field('country'),
>    Field('city')]
>
>
> db.auth_user.country.requires = IS_IN_SET(COUNTRIESANDCITIES.keys())   # 
> this is working as expected
> db.auth_user.city.requires = 
> IS_IN_SET(COUNTRIESANDCITIES[request.post_vars.country])    # this is not 
> working - getting KeyError
>
>
> I created a module called countriesandcities.py (shortened here for 
> illustration):
>
> COUNTRIESANDCITIES = {'Australia':["Adelaide", "Brisbane", "Perth", 
> "Melbourne"], 'Malaysia': ["Ampang", "Kuching", "Miri", "Kuala Lumpur"]}
>
> What I am trying to achieve is this:
> (1) When user is registering, user will select the 'country' from a drop 
> down list which is required to be one of the keys in the COUNTRIESANDCITIES 
> dict. This part works fine.
>
> (2) Based on the selection in (1) above, the user will now select the 
> 'city' from a drop down list and this drop down list should just show the 
> 'values' (cities) for the 'key' (country) previously selected.
>
> However, part (2) doesn't seem to be working and I am getting this error:
>
> <type 'exceptions.KeyError'>(None)
>
> That seems to suggest that there is no existing 'key' which would seem to 
> imply that   request.post_vars.country  is not working.
>
> Any help would very much be appreciated.
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to