2014-10-06 50 views
-3

的下面的Django視圖不斷引發錯誤異常值:列表分配索引超出範圍

異常值:全局名稱「圖像」沒有定義

views.py

PATH_ONE_IMAGES = ['P1D1.jpg', 'P2D2.jpg', 'P3D3.jpg', 'P4D4.jpg', 'P5D5.jpg', 'P6D6.jpg', 'P7D7.jpg', 'P8D8.jpg', 'P9D9.jpg'] 


class SurveyWizardOne(SessionWizardView):      
    def get_context_data(self, form, **kwargs): 
     context = super(SurveyWizardOne, self).get_context_data(form, **kwargs) 
     if self.steps.current in ['5','6','7','8','9','10','11','12','13','14','15','16']: 
      print '\nThe available list of Path_One images is', PATH_ONE_IMAGES 

      images = [] 
      step = int(self.steps.current) 

      if step in (5, 6, 7): 
       images[step - 5] = image = random.choice(PATH_ONE_IMAGES)   
       PATH_ONE_IMAGES.remove(image) 
       context['display_image'] = image 

      elif step == 8: 
       context['first_image'] = images[0] 
       context['second_image'] = images[1] 
       context['third_image'] = images[2]    

      elif step in (9, 10, 11): 
       images[3 + step - 9] = image = random.choice(PATH_ONE_IMAGES)   
       PATH_ONE_IMAGES.remove(image) 
       context['display_image'] = image 

      elif step == 12: 
       context['fourth_image'] = images[3] 
       context['fifth_image'] = images[4] 
       context['sixth_image'] = images[5] 

      elif step in (13, 14, 15): 
       images[6 + step - 13] = image = random.choice(PATH_ONE_IMAGES)   
       PATH_ONE_IMAGES.remove(image) 
       context['display_image'] = image 

      else:# self.steps.current == '16': 
       context['fourth_image'] = images[6] 
       context['fifth_image'] = images[7] 
       context['sixth_image'] = images[8] 

      steps = ['5','6','7','9','10','11','13','14','15']    

      context.update({'steps': steps}) 

     return context 

當我使用

來定義'圖像'時
 .... 
     if self.steps.current in ['5','6','7','8','9','10','11','12','13','14','15','16']: 

      images = [] 
      step = int(self.steps.current) 

      if step in (5, 6, 7): 
     .... 

我得到

異常值:列表分配索引超出範圍

如果我值添加到它

images = [0,1,2,3,4] 

他們變得硬編碼並沒有考慮的價值的PATH_ONE_IMAGES

任何人都可以看到我在這裏做錯了什麼?如何定義圖像數組,使其列表分配索引不超出範圍,因此可以更新?

回答

1

您已發佈但可能會丟失重要信息,該代碼,假設沒有什麼重要的缺失,我認爲罪魁禍首是你的代碼(當去除無用的細節下)看起來像:

images = [] 
for x in something: 
    images[index] = x 

這是不是有效。使用正常分配不能添加項目清單:

In [1]: images = [] 
    ...: images[0] = 1 
    ...: 
--------------------------------------------------------------------------- 
IndexError        Traceback (most recent call last) 
<ipython-input-1-d32b85fdcde7> in <module>() 
     1 images = [] 
----> 2 images[0] = 1 
     3 

IndexError: list assignment index out of range 

當您使用name[index] = value符號只能修改值在指數index但這種指數必須在列表中已經存在,否則會引發IndexError。只用當

In [2]: images = [] 

In [3]: images.append(1) 

In [4]: images 
Out[4]: [1] 

In [5]: images.insert(0, 2) 

In [6]: images 
Out[6]: [2, 1] 

=符號可以改變一個列表的大小:

您必須使用append在列表的末尾添加,或使用insert在特定索引添加項切片:

In [7]: images[2:10] = range(8) 

In [8]: images 
Out[8]: [2, 1, 0, 1, 2, 3, 4, 5, 6, 7] 

實際上,通常使用切片的操作不會引起IndexError。即使images是空列表,您可以評估images[100:1000],它將簡單地評估爲空列表(因爲索引超出範圍,它會返回空列表),而images[100]會產生IndexError)。

+0

嗨Bakuriu。我添加了模式代碼,以便您可以看到我想要實現的目標。 – Deepend 2014-10-06 17:32:09

+0

@Deepend是的,從現在發佈的代碼中可以清楚地看出問題是我在回答中描述的。你有'圖像= []',之後'用於步驟(5,6,7):images [step-5] = something'這意味着循環嘗試執行'images [0] = something',正如我所說,由於'圖像'是空的,會引發錯誤。但是我並沒有真正明白你想要達到什麼目的。你的代碼試圖在某個時候使用'images [8]',但'images'在那段代碼中不會有8個元素。 – Bakuriu 2014-10-06 20:34:51

+0

我做了一個非常糟糕的工作解釋這一點。基本上,用戶在每個頁面上看到隨機選擇的不同圖像。在三頁這樣的頁面之後,用戶意圖再次看到前三張圖像。 (數據驗證頁面)發生三次,或三個三個組。 – Deepend 2014-10-06 20:38:30

0

使用append功能的元素,因爲您的step開始從5添加到列表

爲如

images=[] 
images.append(5) 
images.append(6) 
print images 
[5,6] 

意味着images列表從零開始,這是correcty使用append語句

插入

所以代碼應該是

f step in (5, 6, 7): 
       images.append(random.choice(PATH_ONE_IMAGES)) 
       PATH_ONE_IMAGES.remove(image) 
       context['display_image'] = image