2017-04-25 4 views
1

여러 가지 유형의 데이터를 입력하는 Python 3.5에서 CherryPy를 사용하여 간단한 웹 양식을 만들려고합니다 (sqlite3 데이터베이스와 비교). 확인란을 선택하지 않으면 기본값 null 값이없는 것으로 가정합니다 (예 : 가정). 그것은 "on"이거나 존재하지 않습니다. 빈 상자를 자동으로 '없음'으로 설정하도록 양식을 어떻게 바꿀 수 있습니까?CherryPy 웹 양식 : 선택하지 않은 경우 확인란이 오류가 발생합니다.

는 HTML 체크 박스의 일반 행동의
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

, 당신은 것을 처리 할 수 ​​있습니다 : 이것은 하나의 tickbox 선택하지 않은 웹 페이지에 출력이

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" 

입니다 : 여기 코드 (섹션)입니다 기본 인수가 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