Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6929549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:19:56+00:00 2026-05-27T11:19:56+00:00

I need some help to have a widget work correctly in a web2py app.

  • 0

I need some help to have a widget work correctly in a web2py app.
By the way, once corrected, I think it could be useful to other users…

I built a SELECT widget to expose regions and their sub-regions (‘departement’) so that the user may have the choice.
For instance: a region A is divided into departement 11 and departement 12 and so on.
The user can choose either a region or a specific departement (sub-region).

The widget works correctly except that when submitted, the form returns errors:
form.errors: “r_name: no data”.
Below is a working example to show the problem.

Could anybody help me having this widget work correctly without returning errors, please. Since I don’t understand what’s wrong…
I would greatly appreciate your kind help.
Thanks a lot in advance.
Sorry for the formatting, but it’s the first time I post here … 😉

Dominique

in the model:

     db.define_table('region',
Field('r_name', 'string', length=250, required=True)
)  
db.define_table('departement',
Field('d_name', 'string', length=250, required=True),
Field('d_code', 'string', length=3, required=True),
Field('d_region', db.region)
)
db.define_table('region',
Field('r_name', 'string', length=250, required=True)
)  
db.define_table('departement',
Field('d_name', 'string', length=250, required=True),
Field('d_code', 'string', length=3, required=True),
Field('d_region', db.region)
)

def region_widget(f,v,_class= "options"):
"""
Widget  that shows regions and departements in a SELECT.
Can be used with SQLFORM and SQLFORM.factory
Usage:
db.region.r_name.widget = region_dptt_select_widget
"""
region_dptt = db(db.region.id==db.departement.d_region)\
                .select(db.region.r_name, db.region.id, db.departement.d_name, db.departement.d_code, db.departement.id, \
                orderby=db.region.r_name, cache=(cache.ram,6))

   def prepare_select(rows):
    """
    Creates a list of tuples to be used in the SELECT helper:
    ['Region A', OPTGROUP('SubRegion 1','SubRegion 2', 'SubRegion 3'),
    'Region B', OPTGROUP('SubRegion 4','SubRegion 5', 'SubRegion 6')]
    """
    l_r=[]
    for row in rows:
        r = row.region.r_name
        if row.region.r_name not in l_r:
            l_r.append(row.region.r_name)
    interm_list=[]
    for reg in l_r:
       l_sr=[]
       for row in rows:
            if reg == row.region.r_name:
                reg_id = row.region.id
                dep = row.departement.d_code + ' ' + row.departement.d_name
                dep_id = row.departement.id
                l_sr.append(OPTION(dep,_value=row.departement.d_code)) 
       option_reg = OPTION(reg, _value= 'R'+str(reg_id))
       interm_list.append((option_reg,l_sr))
    res=[]
    for reg, l_sr in interm_list:
        res.append(reg)
        res.append(OPTGROUP(*l_sr))
    return res
return SELECT(*prepare_select(region_dptt),
              **dict(_name='region_to_search', _value=v, _id='region_to_search', _type = 'string', _class="options"))

in the controller:

  def test():
db.region.r_name.widget = region_widget
fields=['r_name']
labels = {'r_name':T('')}
form= SQLFORM(db.region, fields = fields, labels = labels)
a=None
if form.accepts(request.vars, session, formname='test_form', dbio=False):
    a = form.vars
elif form.errors:
    a = form.errors
return dict(form=form, a = a)

def insertions():# TO RUN one time to fill regions and departments in the db
db.region.insert(r_name="Region A")
db.region.insert(r_name="Region B")
db.departement.insert(d_name="SubRegion 1", d_code="11", d_region=1)
db.departement.insert(d_name="SubRegion 2", d_code="12",  d_region=1)
db.departement.insert(d_name="SubRegion 3", d_code="21",  d_region=2)
db.departement.insert(d_name="SubRegion 4", d_code="22",  d_region=2)
db.departement.insert(d_name="SubRegion 5", d_code="25",  d_region=2)
db.departement.insert(d_name="SubRegion 6", d_code="26",  d_region=2) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:19:57+00:00Added an answer on May 27, 2026 at 11:19 am

    Your widget sets _name='region_to_search', but SQLFORM expects the form input field name (and therefore the associated variable in request.vars) to be the same as the name of the field in the db table used to generate the form (which is ‘r_name’). So, try changing your widget code to _name='r_name'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help with a work project I have been assigned. At the
I need some help. I have a 2 pages in my app. Page #1
Need some help about with Memcache. I have created a class and want to
Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
I need some help from the shell-script gurus out there. I have a .txt
I need some help about WCF and authorization. Currently I have a client which
I need some help in solving this problem. We have a large amount of
I need some help with an array and a foreach loop, I have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.