2013-08-02 91 views
3

我有問題,與Django外殼的奇怪行爲。我有這樣的代碼:內聯django外殼與python外殼的變量範圍

fields = ('name', 'description', 'long_description', 'foot_description') 
a = 1 
dict((field, a) for field in fields) 

當我從蟒蛇運行殼它給我正確的字典。但是當我從django shell運行它時,我得到:

--------------------------------------------------------------------------- 
NameError         Traceback (most recent call last) 
/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in  <module>() 
----> 1 dict((field, a) for field in fields) 

/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <genexpr>((field,)) 
----> 1 dict((field, a) for field in fields) 

NameError: global name 'a' is not defined 

我的問題很簡單:爲什麼?

+1

似乎無法將其複製到我的外殼上。 :/ –

+0

我可以在Django 1.3 IPython shell中複製這個錯誤。 – aychedee

+0

@aychedee我不能......'Out [3]:{'description':1,'foot_description':1,'long_description':1,'name':1}' –

回答

2

它似乎是a known issue and fixed in Django 1.6

目前,票據中有一個建議的解決方法。 「抓住以下行(從這裏https://github.com/django/django/blob/master/django/core/management/commands/shell.py)以及與此取代目前實施(...)」:

def ipython(self): 
    try: 
     from IPython.frontend.terminal.ipapp import TerminalIPythonApp 
     app = TerminalIPythonApp.instance() 
     app.initialize(argv=[]) 
     app.start() 
    except ImportError: 
     # IPython < 0.11 
     # Explicitly pass an empty list as arguments, because otherwise 
     # IPython would use sys.argv from this script. 
     try: 
      from IPython.Shell import IPShell 
      shell = IPShell(argv=[]) 
      shell.mainloop() 
     except ImportError: 
      # IPython not found at all, raise ImportError 
      raise 

您也可以嘗試python manage.py shell --plain,從同一票評論。