2014-09-28 16 views
0

以下是Django SessionWizardView的get_context_update方法,該方法用於將多個變量(包括display_image)返回給多頁表單的內部頁面。但是我無法以這樣的方式分配變量five_image,six_imageseven_image以至於他們堅持。存儲3個獨立頁面中使用的變量的值以供第四頁使用

因此,在第5,6和7頁上,用戶每次看到不同的圖像(display_image)。

但是在第8頁上,我想向用戶展示他們之前看過的三張圖像都是哪一頁。

所以我想存儲的display_image的步驟5,6和7的值,並將其添加到我的context.update

但是沒有我的嘗試已經破解它。在下面的情況下,沒有圖像可見第8頁。我甚至明白,爲什麼是因爲if self.steps.current == '5': (6 and 7)不等於8。但是,如果我讓他們== 8,那麼它只顯示所有三個相同的圖像,然後將其從path_one_images

所以我的問題是:如何存放的每一個顯示頁面5,6和7,所以我可以顯示8頁上的所有三個的display_image值?

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']: 

      display_image = random.choice(path_one_images) 

      if self.steps.current == '5': 
       context['five_image'] = display_image 

      if self.steps.current == '6': 
       context['six_image'] = display_image 

      if self.steps.current == '7': 
       context['seven_image'] = display_image 

      first_three = ['5','6','7'] 

      context.update({'display_image': display_image, 
          'first_three': first_three, 
          }) 

      path_one_images.remove(display_image) 
     return context 

以上是爲了每個在我的HTML模板

<img src="{% static "survey/images/pathone/" %}{{five_image}}"/> 
<img src="{% static "survey/images/pathone/" %}{{six_image}}"/> 
<img src="{% static "survey/images/pathone/" %}{{seven_image}}"/> 

任何幫助下面的是大加讚賞返回一個單獨的值。

+2

您是否嘗試過在會話中儲存顯示的圖像的元組,再後來訪問它? (即request.session ['images_shown'] =(...)) – gorus 2014-09-28 20:20:01

+0

爲什麼你的'path_one_images'中的''P5D5.jpg /''上有一個流浪的'/'? (PS,從標題我希望有人正在編寫一個網站在Python和Forth的組合:) :) – abarnert 2014-09-28 23:47:03

+0

@abarnert我會糾正一個錯誤。 – Deepend 2014-09-28 23:54:53

回答

0

所以我最終通過顛倒我的邏輯來解決我的問題。而不是試圖使display_image的輸出等於我將使用的另一個變量,我換個方式。

我使用單獨的變量,我從圖像列表中移除的每個圖像(first_imagesecond_imagethird_image),然後由顯示圖像取該值。這仍然留下我以後可以使用的原始價值。

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']: 

       global first_image, second_image, third_image 

       if self.steps.current == '5': 
        first_image = random.choice(path_one_images)   
        path_one_images.remove(first_image) 
        display_image = first_image 
        context['display_image'] = display_image 

       if self.steps.current == '6': 
        second_image = random.choice(path_one_images)  
        path_one_images.remove(second_image) 
        display_image = second_image 
        context['display_image'] = display_image 

       if self.steps.current == '7': 
        third_image = random.choice(path_one_images)    
        path_one_images.remove(third_image) 
        display_image = third_image 
        context['display_image'] = display_image 



       if self.steps.current == '8': 
        context['first_image'] = first_image 
        context['second_image'] = second_image 
        context['third_image'] = third_image 

       first_three = ['5','6','7']    

       context.update({'first_three': first_three 
           }) 
      return context 

希望這可以幫助有人爲

相關問題