返回SessionWizardView中的变量

时间:2014-08-30 18:16:42

标签: django django-forms

我正在尝试将变量返回到SessionWizardView内的特定页面,但没有太多运气。我的目标是通过使文件名为变量和<image src=属性的最后部分来向用户显示随机图像。

问题是以下image()功能不会发送&#34; display_image wizard_form.html页面的SessionWizardView变量。

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的相关部分。我在{% if wizard.steps.current == '5' %}处有一小部分逻辑taken from this excellent answer,它向用户显示了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 个答案:

答案 0 :(得分:1)

您还没有显示任何调用image方法的代码,因此我不知道它是如何发送&#39;页面的display_image变量。

如果要向模板上下文添加变量,建议您尝试覆盖get_context_data方法。

相关问题