如何在 python web.py 中使用单选按钮
问题描述:
我正在使用 web.py 框架来创建一个简单的 Web 应用程序
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)
当我使用 url localhost:8080
运行代码时,我看到以下错误
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
答
查看 source,看起来您必须为所有项目使用一个 Radio
构造函数,因为同一个 Radio
对象实际上会生成多个 元素.
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.
试试像::
project_details = form.Form(
form.Radio('details', ['Home Page', 'Content', 'Contact Us', 'Sitemap']),
)