2014-08-30 81 views
0

我想將一個變量返回到SessionWizardView中的某個特定頁面,但沒有太多運氣。我的目標是通過將其文件名稱變量和<image src=屬性的最後部分顯示給用戶隨機圖像。在SessionWizardView中返回一個變量

問題是,以下image()函數不會將display_image變量「發送」到SessionWizardView wizard_form.html頁面。

forms.py

class SurveyFormF(forms.Form): 

    def image(request):   
     path_one_images = ['P1D1.jpg', 'P2D2.jpg', 'P3D3.jpg'] 

     display_image = random.choice(path_one_images)  
     return render(request, 'wizard_form.html', {'display_image': display_image})  

nothing = forms.CharField(required=False, widget=forms.HiddenInput) 

wizard_form.html

爲了減少空間我只包括wizard_form.html的相關部分。我有一小部分邏輯taken from this excellent answer{% if wizard.steps.current == '5' %},它向SessionWizzardView的第5頁上的用戶顯示下面的HTML內容。

<p>Page: {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p> 

<form action="" method="post">{% csrf_token %} 
      <table> 
       {{ wizard.management_form }} 
        {% if wizard.form.forms %} 
         {{ wizard.form.management_form }} 
         {% for form in wizard.form.forms %} 
          {{ form }} 
         {% endfor %} 
        {% else %} 
         {{ wizard.form }} 
       {% endif %} 
      </table> 

     {% if wizard.steps.current == '5' %} 

       <h1>Experiment</h1> 
       <p>Lorem ipsum dolor sit amet</p> 

       {% load staticfiles %}                

        <img src="{% static "survey/images/pathone/" %}"{{display_image}}/> 

        <section> 
         <span class="tooltip"></span> 
         <div id="slider"></div> 
         <span class="volume"></span> 
        </section>  
     {% endif %} 

當然的wizard_form.html頁面被埋葬在該項目在templates/formtools/wizard/wizard_form.htmlDocumentation here),但我認爲它更與SWV而不是HTML文件的位置的問題。

任何幫助一如既往,非常感謝。謝謝。

回答

1

您還沒有顯示任何調用image方法的代碼,所以我不知道它是如何將display_image變量「發送」到頁面的。

如果你想添加一個變量到模板上下文,我建議你嘗試覆蓋get_context_data方法。