2009-09-07 94 views
1

我有這個類:IF語句導致內部服務器錯誤與webpy

class View(object): 
    def main_page(self, extra_placeholders = None): 
     file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl' 

     placeholders = { 'site_name' : 'pypular' } 

     # If we passed placeholders vars, append them 
     if extra_placeholders != None: 
      for k, v in extra_placeholders.iteritems(): 
       placeholders[k] = v 

我的代碼中的問題上面是if語句

正如你可以看到,該函數接受一個參數(extra_placeholders)這是一個字典。

如果我不將參數傳遞給main_page(),

if extra_placeholders == None: 
    return 'i executed' 

運行正常。然而,

if extra_placeholders != None: 
    return 'i cause error' 

不起作用。它會導致500內部服務器錯誤。爲什麼?

回答

1

,你應該使用,而不是

if !(extra_placeholders is None) : 

編輯:爲了反映點評:

看來(感謝),您還可以使用:

if extra_placeholders is not None : 

更新:原單鏈接現在死了,所以這個SO回答是一個很好的參考:https://stackoverflow.com/a/3289606/30225

+0

謝謝。 「不是沒有」似乎也做到了。 – sqram 2009-09-07 07:02:29

+0

並感謝您的鏈接。真的澄清了一些事情! – sqram 2009-09-07 07:09:51

+0

這是一個很好的例子,爲什麼外部鏈接可能有幫助,但最相關的信息應該被複制到答案中......因爲鏈接不再可用。 – kmarsh 2017-02-24 14:02:07

相關問題