2011-09-28 83 views
2

我有這樣的代碼,Django的嘗試除了不工作

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 

編輯 經過進一步調查 - 也就是說,手動(這工作),我覺得有什麼東西了以代碼從陣列 - 即使輸入有效的代碼兩者都是相同的數據類型(字符串)的事實。從文件

所以newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact = "econ")的作品,但newClassName = CourseNameAndCodeAssocition.objects.get(departmentCode__iexact = nameAndNumberStore[0]),其中nameAndNumberStore[0]持有經濟學

+2

你有沒有試過,你知道...讓異常通過? –

+0

我注意到你的代碼片段沒有任何導入。也許如果你向我們展示你的整個控制檯會話,以及影響你的環境的所有代碼,我們可以提供幫助。 – Marcin

+0

呃,我正在導入BeautifulSoup,re,urllib2和cookielib - 這個函數有很多函數被整體調用 - 但我會發布整個東西 你是什麼意思讓異常通過?我對編程有點新奇 - 我不需要去捕捉它嗎? – praks5432

回答

6

請修改代碼這一點,運行它,並告訴我們您做了什麼異常:

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

而且,這可能有助於在您的盒子上安裝調試器。我可以推薦Eclipse與PyDev結合使用,但這是個人選擇。那裏有很多很棒的選擇。

Eclipse IDE - download the basic Java version of 120MB

then install this plugin on top of it - Pydev

+0

好的 - 我有個例外 - newClassName什麼也沒有 - 這是因爲departmentCode__iexact沒有找到它的意思是找到什麼 - 當存儲的代碼是「ECON」時,我傳入了一個代碼,如'econ' - 我怎麼能做一個不區分大小寫? – praks5432

+0

你說得對。 Django規範說,iexact是你應該使用的。你能確定你沒有拖曳空白嗎?嘗試使用__contains進行測試。 HTTPS://docs.djangoproject。com/en/dev/ref/models/querysets /#std:fieldlookup -exxact – Michael

+0

嗨praks5432,你有沒有確定你沒有尾隨空格?您是否嘗試__icontains而不是__iexact? – Michael

2

將其更改爲:

except CourseNameAndCodeAssociation.DoesNotExist: 

創建獲取自己DoesNotExist例外,擴展核心ObjectDoesNotExist例外,每個模型。

此外,最好的方法是隻使用try ... except圍繞您期望失敗的準確線條。更Python的方式來寫,你有什麼會有:

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) 

也請注意,在Python的約定是使用lowercase_underscored_names變量,屬性和函數名和CamelCaseNames的類名(例如名稱是變量或屬性)。