2014-08-30 60 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方法。