2017-04-25 56 views
1

我正试图在Python 3.5(使用sqlite3数据库进行比较)中使用CherryPy生成一个简单的Web窗体,它需要输入不同类型的数据。当复选框未被选中时,它会产生一个错误(我假设)没有默认的空值;它或者是'开'或者是不存在的。如何更改我的表单以便自动将空框设置为“无”? 下面是代码(部分):CherryPy 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" 

这是网页上的输出,当一个tickbox没有被选中:

404 Not Found 

Missing parameters: training 

Traceback (most recent call last): 
    File "C:\Users\Anna\AppData\Local\Programs\Python\Python35\lib\site-packages\cherrypy\_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:\Users\Anna\AppData\Local\Programs\Python\Python35\lib\site-packages\cherrypy\_cprequest.py", line 670, in respond 
    response.body = self.handler() 
    File "C:\Users\Anna\AppData\Local\Programs\Python\Python35\lib\site-packages\cherrypy\lib\encoding.py", line 221, in __call__ 
    self.body = self.oldhandler(*args, **kwargs) 
    File "C:\Users\Anna\AppData\Local\Programs\Python\Python35\lib\site-packages\cherrypy\_cpdispatch.py", line 66, in __call__ 
    raise sys.exc_info()[1] 
    File "C:\Users\Anna\AppData\Local\Programs\Python\Python35\lib\site-packages\cherrypy\_cpdispatch.py", line 64, in __call__ 
    test_callable_spec(self.callable, self.args, self.kwargs) 
    File "C:\Users\Anna\AppData\Local\Programs\Python\Python35\lib\site-packages\cherrypy\_cpdispatch.py", line 163, in test_callable_spec 
    raise cherrypy.HTTPError(404, message=message) 
cherrypy._cperror.HTTPError: (404, 'Missing parameters: training') 

回答

2

这就是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" 
+0

谢谢!我在之前的方法调用中尝试过默认的'training = None'参数,但没有成功,可能我的语法错了。我使用了第二种解决方案,它工作得很好。 – fianchi04