장고

2011-09-28 3 views
2
이 코드를

,장고

try: 
     print "what" 
     newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact =   nameAndNumberStore[0]) 
     print newClassName 
    except: 
     print "HAHA" 

작동하지 않음 제외보십시오이 항상 인쇄 "하하"나는 콘솔에서 newClassName = ... 코드를 실행했는데이 일을 있다는 사실에도 불구하고.

왜 이런 일이 발생합니까?

편집

def newGetAllInformation(searchTerm): 
nameAndNumberStore = modifySearchTerm(searchTerm) 
urlStore = modifyUrl(nameAndNumberStore) # need to make the change here -- why not I go to the site, check for Course name - if that is not there switch, if it is then scrape 
soup = getHtml(urlStore) 
storeOfBooks = [] 
storeOfBooks = scrape(soup,nameAndNumberStore) 
print nameAndNumberStore[0] 
try: 
    newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact = nameAndNumberStore[0]) 
    nameAndNumberStore = modifySearchTerm(newClassName.departmentName + " " + nameAndNumberStore[1]) 
    urlStore = modifyUrl(nameAndNumberStore) 
    soup = getHtml(urlStore) 
    storeOfBooks = scrape(soup,nameAndNumberStore) 

except: 
    print "HAHA" 

return storeOfBooks 

편집 추가 조사 후 - 즉, 수동 (이 근무), 내가 뭔가 업에도 불구하고 array-의 코드를 복용가 있다고 생각 유효한 코드를 입력 둘 다 동일한 데이터 유형 (문자열)이라는 사실. nameAndNumberStore[0]경제학

+2

시도해 보셨습니까? 예외 처리를 하시겠습니까? –

+0

코드 스 니펫에 가져 오기가 없습니다. 어쩌면 전체 콘솔 세션과 환경에 영향을 미치는 모든 코드를 보여 주면 도움이 될 것입니다. – Marcin

+0

어, BeautifulSoup, re, urllib2 및 cookielib을 가져옵니다. 전체적으로이 함수에 의해 호출되는 함수가 많이 있지만 전체를 게시합니다. 예외를 통해 무엇을 의미합니까? 나는 프로그래밍에 익숙하지 않은가? 잡을 필요가 없는가? – praks5432

답변

6

을 보유하고

그래서 newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact = "econ") 파일에서 작동하지만 newClassName = CourseNameAndCodeAssocition.objects.get(departmentCode__iexact = nameAndNumberStore[0]), 여기에 코드를 수정하여 실행하고 점점 어떤 예외를 알려주세요 또한

try: 
    print "what" 
    newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact =   nameAndNumberStore[0]) 
    print newClassName 
except Exception as e: 
    print "HAHA" 
    print e 

, 디버거를 설치하는 것이 도움이 될 것입니다. PyDev와 함께 Eclipse를 추천 할 수는 있지만 개인적인 선택입니다. 많은 훌륭한 옵션이 있습니다.

Eclipse IDE - download the basic Java version of 120MB

then install this plugin on top of it - Pydev

+0

OK- 예외가 있습니다 - newClassName은 아무 것도 없습니다 - departmentCode__iexact가 그 의미를 찾는 것을 찾지 못해서입니다 - 저장 한 코드가 "ECON"인 경우 'econ'과 같은 코드를 전달하고있었습니다. 어떻게해야합니까? 대소 문자를 구분하지 않습니까? – praks5432

+0

그게 맞습니다. Django 스펙은 iexact가 사용해야한다고 말합니다. 흰 공백이 없도록 할 수 있습니까? 테스트를 위해 __icontains를 시도하십시오. https : //docs.djangoproject.com/en/dev/ref/models/querysets/# std : fieldlookup-iexact – Michael

+0

안녕 praks5432, 당신은 뒷 배경 공백이 있는지 확인 했습니까? __iexact 대신 __icontains를 시도 했습니까? – Michael

2

변경 그것에 :

except CourseNameAndCodeAssociation.DoesNotExist: 

당신은 핵심 ObjectDoesNotExist 예외를 확장 자신의 DoesNotExist 예외를 얻을 작성하는 각 모델.

가장 좋은 방법은 실패 할 것으로 예상되는 정확한 줄 주위에만 try ... except을 사용하는 것입니다. 더 파이썬 방법은 당신이 무엇을 쓸 수있을 것입니다 :

department_code = name_and_number_store[0] 
class_names = CourseNameAndCodeAssociation.objects.all() 
try: 
    new_class_name = class_names.get(departmentCode__iexact=department_code) 
except CourseNameAndCodeAssociation.DoesNotExist: 
    print "HAHA" 
else: 
    search_term = u'%s %s' % (new_class_name.departmentName, 
           name_and_number_store[1]) 
    name_and_number_store = modify_search_term(search_term) 
    url_store = modify_url(name_and_number_store) 
    soup = get_html(url_store) 
    store_of_books = scrape(soup, name_and_number_store) 

파이썬의 규칙은, 변수 lowercase_underscored_names을 사용하는 것입니다 인스턴스 이름 중 하나를 변수 (속성 및 함수 이름과 클래스 이름에 대한 CamelCaseNames 있습니다 또는 속성).