Skip to content Skip to sidebar Skip to footer

How To Use Radio Buttons In Python Web.py

I am using web.py framework to create a simple web application I want to create a radio button so I wrote the following code from web import form from web.contrib.auth import DBAut

Solution 1:

Looking at the source, it looks like you have to use one Radio constructor for all of your items as the same Radio object will actually generate multiple <input> elements.

Try something like::

project_details = form.Form( 
    form.Radio('details', ['Home Page', 'Content', 'Contact Us', 'Sitemap']),
    )

Solution 2:

Here's what I was able to decipher. checked='checked' seems to select a random (last?) item in the list. Without a default selection, my testing was coming back with a NoneType, if none of the radio-buttons got selected.

project_details = form.Form(
  form.Radio('selections', ['Home Page', 'Content', 'Contact Us','Sitemap'], checked='checked'),
  form.Button("Submit") 
)

To access your user selection as a string...

result = project_details['selections'].value

If you want to use javascript while your template is active, you can add, onchange='myFunction()' to the end of the Radio line-item. I'm also assigning an id for each element, to avoid frustration with my getElementById calls, so my declaration looks like this.

 project_details = form.Form(
   form.Radio('selections', ['Home Page', 'Content', 'Contact Us','Sitemap'], checked='checked', onchange='myFunction()', id='selections'),
   form.Button("Submit") 
)

Post a Comment for "How To Use Radio Buttons In Python Web.py"