樱桃皮网络表单:复选框在未选中时会产生错误



我正在尝试使用Python 3.5中的CherryPy(与sqlite3数据库进行比较)生成一个简单的Web表单,该表单需要一些不同类型的数据输入。当复选框未选中时,它会产生一个错误,因为(我假设)没有默认的空值;它要么"打开",要么不存在。如何更改我的表单,使其自动将空框设置为"无"? 这是代码(部分):

class startScreen(object):
@cherrypy.expose
def index(self):
return """<form method="post" action="search">
Job Title:<br>
<input type="text" name="title"><br>
Employer name:<br>
<input type="text" name="employer"><br>
Minimum Starting Salary:<br>
<input type="number" name="minsal"><br>
Contract Hours Per Week:<br>
<input type="number" name="hpwMin">
<input type="number" name="hpwMax"><br>
Start Date:<br>
<input type="date" name="startDate"><br>
<!--jobtype drop down menu--!>
Contract Length (months):<br>
<input type="number" name="CLMin">
<input type="number" name="CLMax"><br>
<!--qualifications list--!>
<!--key skills list--!>
Training Offered:<br>
<input type="checkbox" name="training"><br>
Expenses covered:<br>
<input type="checkbox" name="expenses"><br>
Job benefits:<br>
<input type="checkbox" name="benefits"><br>
Number of days annual holiday: <br>
<input type="number" name="holiday"><br>
Opportunities abroad:<br>
<input type="checkbox" name="abroad"><br>
Date posted: <br>
<input type="date" name="datePosted"><br>

<button type="submit">Submit</button>
</form>
"""
@cherrypy.expose #needed for every page
def search(self, title, employer, minsal, hpwMin, hpwMax, startDate, CLMin, CLMax, training, expenses, benefits, holiday, abroad, datePosted):
search.search.searchDBS(title, employer, minsal, hpwMin, hpwMax, startDate, CLMin, CLMax, training, expenses, benefits, holiday, abroad, datePosted)
return "done"

这是取消选中一个复选框时网页上的输出:

404 Not Found
Missing parameters: training
Traceback (most recent call last):
File "C:UsersAnnaAppDataLocalProgramsPythonPython35libsite-packagescherrypy_cpdispatch.py", line 60, in __call__
return self.callable(*self.args, **self.kwargs)
TypeError: search() missing 1 required positional argument: 'training'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersAnnaAppDataLocalProgramsPythonPython35libsite-packagescherrypy_cprequest.py", line 670, in respond
response.body = self.handler()
File "C:UsersAnnaAppDataLocalProgramsPythonPython35libsite-packagescherrypylibencoding.py", line 221, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "C:UsersAnnaAppDataLocalProgramsPythonPython35libsite-packagescherrypy_cpdispatch.py", line 66, in __call__
raise sys.exc_info()[1]
File "C:UsersAnnaAppDataLocalProgramsPythonPython35libsite-packagescherrypy_cpdispatch.py", line 64, in __call__
test_callable_spec(self.callable, self.args, self.kwargs)
File "C:UsersAnnaAppDataLocalProgramsPythonPython35libsite-packagescherrypy_cpdispatch.py", line 163, in test_callable_spec
raise cherrypy.HTTPError(404, message=message)
cherrypy._cperror.HTTPError: (404, 'Missing parameters: training')

这是 html 复选框的常规行为,您可以使用默认参数来处理它:training=None或使用 kwargs 并查找键。

对于第一个选项,您的公开方法是:

@cherrypy.expose #needed for every page
def search(self, title, employer, minsal, hpwMin, hpwMax,
startDate,  CLMin, CLMax, expenses,
benefits, holiday, abroad, datePosted, training=None):
# "training" will be None, if the checkbox is not set
# you can verify with something like:
# if training is None:
#    ...
search.search.searchDBS(
title, employer, minsal, hpwMin, hpwMax,
startDate, CLMin, CLMax, training, expenses,
benefits, holiday, abroad, datePosted)
return "done"

另一种选择(从我的角度来看更好,因为这些是方法的很多参数,您可以使用**params替代方案:

@cherrypy.expose #needed for every page
def search(self, **params):
fields = ['title', 'employer', 'minsal', 'hpwMin',
'hpwMax', 'startDate', 'CLMin', 'CLMax',
'training', 'expenses','benefits', 'holiday',
'abroad','datePosted']
# the params.get, will try to get the value if the field
# and if is not defined, then it will return None
search.search.searchDBS(*[params.get(f, None) for f in fields])
# alternative approach without passing specific fields
#if 'training' not in params: 
#    params['training'] = None
#search.search.searchDBS(**params)
return "done"

相关内容

  • 没有找到相关文章

最新更新