# Reduce and condense a non standard SessionWizardView

As I'm slowly getting the hang of Python and Django I'm becoming increasingly worried about the volume/quality of code I have. This is my first time building a Python/Django Application and I'm learning as I go.

The only section I'm really looking for help on is the below get_context_data of my SessionWizardView in views.py I will have to create 9 of these and I feel there is a better way of doing it. A SessionWizardView is used to split a form over multiple pages for the user. get_context_data is used to return a varible to an internal page of the SessionWizardView. In the below example I have interrupted the standard SessionWizrdView pages with 9 additional internal steps (5 - 13) where the user is asked to rate a serious of images using a JS slider bar.

I included the other code elements so anyone could see what I was trying to do.

I would like to know is there a better, more condensed way of coding the below get_context_data of my SessionWizardView.

I would also like to keep it readable and easily maintainable for a beginner programmer.

views.py

class SurveyWizardOne(SessionWizardView):

def get_context_data(self, form, **kwargs):
context = super(SurveyWizardOne, self).get_context_data(form, **kwargs)
if self.steps.current == '5':
display_image = random.choice(path_one_images)
context.update({'display_image': display_image})
path_one_images.remove(display_image)

elif self.steps.current == '6':
display_image = random.choice(path_one_images)
context.update({'display_image': display_image})
path_one_images.remove(display_image)

elif self.steps.current == '7':
display_image = random.choice(path_one_images)
context.update({'display_image': display_image})
path_one_images.remove(display_image)

elif self.steps.current == '8':
display_image = random.choice(path_one_images)
context.update({'display_image': display_image})
path_one_images.remove(display_image)

elif self.steps.current == '9':
display_image = random.choice(path_one_images)
context.update({'display_image': display_image})
path_one_images.remove(display_image)

elif self.steps.current == '10':
display_image = random.choice(path_one_images)
context.update({'display_image': display_image})
path_one_images.remove(display_image)

elif self.steps.current == '11':
display_image = random.choice(path_one_images)
context.update({'display_image': display_image})
path_one_images.remove(display_image)

elif self.steps.current == '12':
display_image = random.choice(path_one_images)
context.update({'display_image': display_image})
path_one_images.remove(display_image)

elif self.steps.current == '13':
display_image = random.choice(path_one_images)
context.update({'display_image': display_image})
path_one_images.remove(display_image)

return context

def done(self, form_list, **kwargs):
global SurveyWizardOneCounter
global TotalMaxCounter
SurveyWizardOneCounter += 1
TotalMaxCounter += 1
return render_to_response('Return_to_AMT.html', {
'form_data': [form.cleaned_data for form in form_list],
})


wizard_form.html

Ive included thsi for the big picture, Im not too worried about my wizard form as I just have the below for each of the steps. It dosent require much if any mainteance.

    {% if wizard.steps.current == '5' %}
<img src="{% static "survey/images/pathone/" %}{{display_image}}" height="600" width="500" style="border:1px solid black;" align="middle"/>
<section>
<span class="tooltip"></span>
<div id="slider"></div>
<span class="volume"></span>
</section>
{% endif %}


forms.py

I have also just included this for the big picture, Its jsut a hidden field necessary for each step of the SurveyWizardView.

class SurveyFormF1(forms.Form):
nothing = forms.CharField(required=False, widget=forms.HiddenInput)

class SurveyFormF2(forms.Form):
nothing = forms.CharField(required=False, widget=forms.HiddenInput)

...
...

• Why 13 different ifs if the code executed is the same? Is this the real code? – Emanuele Paolini Sep 9 '14 at 11:41
• yes it is the real code, The if are for each step e.g. elif self.steps.current == '9': – Deepend Sep 9 '14 at 12:04

Maybe I'm missing something but your get_context_data can be simplified to:

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']:
display_image = random.choice(path_one_images)
context.update({'display_image': display_image})
path_one_images.remove(display_image)
return context


Note also that the list of numbers can be written as map(str,range(5,13+1)) or you could check 5 <= int(self.steps.current) <= 13.

• That works, Ive never used the if in list functionality before. thanks – Deepend Sep 9 '14 at 13:38