How to create a dropdown selection page?

646 views
Skip to first unread message

Adrian Zhang

unread,
Jan 21, 2014, 2:44:20 AM1/21/14
to web...@googlegroups.com
I am a totally stupid beginner of coding, and really confused by lots of posts that explain how to create a drop-down selection.
Here is my question: 
3 tables: nations, shops, brands
the relationships between these tables are:
a country has multiple shops;
a shop has multiple brands;
some of brands are in different shops, for example, brand A is in both shop X and shop Y.
My object is that user can choose shops in a range, select country to see shops belong to this country, select brand to see shops have this brand, and select all to see all shops.

So I have 3 tables: countries, shops and brands. In MVC model, these tables are defined in models/db.py, and I am able to input information in DB admin panel.

db.define_table('countries',
   Field('name'), 
   format = '%(name)s'
   )
db.define_table('shops',
   Field('name'), 
   Field('country_id', 'reference countries'),
   format = '%(name)s'
   )
db.define_table('brands',
   Field('name'),
   Field('shop_id', 'reference shops'),
   Field('story', 'text'),
   Field('address', 'text'),
    Field('opening hours', 'date'),
   format = '%(namecn)s'
   )

But then, how to write in controllers/default.py, and how to create views/default/selection.html? I want selection looks like it has 2 dropdown lists, users choose a country from one of them and submit, or choose brand from another of them and submit. And a link named click to see all shops. Once users make action, then to get a shop list in different range (belong to a country, or have a selected brand, or all of them), and shop list shows shop links. I am afraid it is not clear, see below picture.

selection.html
-------------------------------------------------
_______________       _______       
| select a country |      | submit |
---------------------------      -------------

_______________       _______       
| select a brand    |      | submit |
---------------------------      -------------

See all shops
---------------------------------------------------

if select a country or brand and click submit, or click See all shops

---------------------------------------------------
shops in A country (or shops has brand B, or ALL shops)

shop1
shop2
shop3
shop4
.......
.......
shopn
---------------------------------------------------

once click one of shop's link, users go to final page:


---------------------------------------------------
ShopX


Story: bala bala
address: bala bala
opening hours: bala bala
all brands in this shop:
brand1
brand2
brand3
......
......
brandn
---------------------------------------------------


I read several cases in manual, forum, QA webpages but still not success because I am too stupid that can only understand the good sample in manual (to create a photo blog, or wiki).  Can somebody give me clear codes (in controller and view) for this sample. If  there is already a sample similar to my one, just give me a link please. Thanks a lot !!!!!!!

黄祥

unread,
Jan 21, 2014, 7:37:31 AM1/21/14
to web...@googlegroups.com
had you already take a look at multiple forms per page in web2py book?
ref:

best regards,
stifan

Adrian Zhang

unread,
Jan 21, 2014, 8:09:11 AM1/21/14
to web...@googlegroups.com
Thanks for reply.
Actually SQLFORM is exactly my confusion.I need a drop-down list instead of input form. And I did view several posts about this problem (such as listed below), none of them works for me.


Problem is, non of them explain SQLFORM usage clearly, from drop-down point of view. Where can I find a simple case of drop-down? Just like the simple sample in the manual like photo blog, wiki, those are very useful to a beginner to understand how MVC works.

But for drop-down problem, what should be defended in models/db.py, controllers/default.py and views/default/drop-down.html? Any help is appreciate. 

黄祥

unread,
Jan 21, 2014, 10:53:28 AM1/21/14
to web...@googlegroups.com
i'm sorry, my bad, i focus on your layout before 
selection.html
-------------------------------------------------
_______________       _______       
| select a country |      | submit |
---------------------------      -------------

_______________       _______       
| select a brand    |      | submit |
---------------------------      -------------

See all shops

for learning how sqlform manage it, i think there is on the web2py gluon modules sqlhtml.py

for drop down form field, for start from simple, i think it created automatically if you set the field type as reference or list:reference (it's on models side). or if you want to create it in controllers side, please use the validators (IS_IN_DB(), IS_IN_SET())

please read the book that related with :
models : with field type reference and list:reference
and validators like : IS_IN_DB() and IS_IN_SET()

best regards,
stifan

Adrian Zhang

unread,
Jan 21, 2014, 11:44:51 AM1/21/14
to web...@googlegroups.com
Thank you so much for your continue support. I tried some clue from manual and created a simple drop-down list with a bit defect.will post here tomorrow.
Thank you again !
Have a nice day!

Adrian

黄祥 <steve.van...@gmail.com>于2014年1月21日星期二写道:
--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/6591gnT-GVA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--
发自移动版 Gmail

Tim Richardson

unread,
Jan 21, 2014, 3:02:00 PM1/21/14
to web...@googlegroups.com
to get drop downs you need to set a validator in the field definition. IS_IN_SET is one validator which causes SQLFORM to automatically make a dropdown. so you don't do anything in SQLFORM. Rather you set up your data model when defining tables.

Adrian Zhang

unread,
Jan 21, 2014, 9:24:44 PM1/21/14
to web...@googlegroups.com
Thank you, Tim. My previous confusion comes from which field should be format as IS_IN_SET or IS_IN_DB once 2 tables have many-to-many links or single-to-many links, how to represent related field in controller and view. Previously listed web posts really take confusions to a beginner. So I will post my experience, a simple case to explain that.
Reply all
Reply to author
Forward
0 new messages