Reputation: 26768
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 DBAuth
import MySQLdb as mdb
render = web.template.render('templates/')
urls = (
'/project_details', 'Project_Details',
)
class Project_Details:
project_details = form.Form(
form.Radio('Home Page'),
form.Radio('Content'),
form.Radio('Contact Us'),
form.Radio('Sitemap'),
)
def GET(self):
project_details = self.project_details()
return render.projectdetails(project_details)
When I run the code with url localhost:8080
I am seeing following error
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/web/application.py", line 237, in process
return p(lambda: process(processors))
File "/usr/lib/python2.7/site-packages/web/application.py", line 565, in processor
h()
File "/usr/lib/python2.7/site-packages/web/application.py", line 661, in __call__
self.check(mod)
File "/usr/lib/python2.7/site-packages/web/application.py", line 680, in check
reload(mod)
File "/home/local/user/python_webcode/index.py", line 68, in <module>
class Project_Details:
File "/home/local/user/python_webcode/index.py", line 72, in Project_Details
form.Radio('password'),
TypeError: __init__() takes at least 3 arguments (2 given)
What parameter need to be passed in the radio button in order to avoid this error
Upvotes: 0
Views: 1979
Reputation: 29
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")
)
Upvotes: 0
Reputation: 1085
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']),
)
Upvotes: 1