# A get_context_data method for selecting and updating images displayed to a user over a multi page form

Below are the get_context_data method and the done method of a SesionWizardView. A SessionWizardView is designed to split a form over multiple pages. The get_context_data method is used to provide updated 'context' data to the individual form pages, in this case images selected from a list at random.

Python 2.7.3 | Django 1.6.2

Purpose of the code

I am using the get_context_data method to update a multi page survey with different images, each selected at random, one each per page (5, 6, 7, 9, 10, 11, 13, 14, 15), which are then removed from the list. The user is asked to rate each of these images using a jQuery slider.

After every three pages/images (8, 12, 16) The images the user has already seen are redisplayed and the user is asked to verify their rating for each. And at the end all of the images they have been shown are redisplayed to them.

What I am looking for

The below code appears to work fine but as I am self thought in Python/Django I don't even know what I don't know, if that makes sense. I have received a lot of help form here and from Stack Overflow with specific issues in the past but now I'm just looking to improve my knowledge/abilities.

images = []
slider_DV_values = []

PathMax = 10
TotalMax = 90
TotalMaxCounter = 0

SurveyWizardOneCounter = 0

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','16', '17', '18']:
step = int(self.steps.current)

if step in (5, 6, 7):
image = random.choice(PATH_ONE_IMAGES)
images.insert(step - 5, image)

PATH_ONE_IMAGES.remove(image)
context['display_image'] = image

if step == 5:
self.request.session['first_image'] = images[0]
first_image = self.request.session.get('first_image', 'default_request_session_value')

elif step == 6:
self.request.session['second_image'] = images[1]
second_image = self.request.session.get('second_image', 'default_request_session_value')

elif step == 7:
self.request.session['third_image'] = images[2]
third_image = self.request.session.get('third_image', 'default_request_session_value')

slider_value = self.request.POST.get('slider_value')
if slider_value is not None:                #Not sure why this is different to the others, must check it out
slider_DV_values.insert(step - 5, slider_value)

elif step == 8:
slider_value = self.request.POST.get('slider_value')
if slider_value is not None:
slider_DV_values.insert(step - 5, slider_value)

context['first_image'] = self.request.session['first_image']
context['second_image'] = self.request.session['second_image']
context['third_image'] = self.request.session['third_image']
context['first_slider'] = slider_DV_values[0]
context['second_slider'] = slider_DV_values[1]
context['third_slider'] = slider_DV_values[2]

if step in (9, 10, 11):
image = random.choice(PATH_ONE_IMAGES)
images.insert(step - 6, image)
PATH_ONE_IMAGES.remove(image)
context['display_image'] = image

if step == 9:
self.request.session['fourth_image'] = images[3]
fourth_image = self.request.session.get('fourth_image', 'default_request_session_value')

elif step == 10:
self.request.session['fifth_image'] = images[4]
fifth_image = self.request.session.get('fifth_image', 'default_request_session_value')

elif step == 11:
self.request.session['sixth_image'] = images[5]
sixth_image = self.request.session.get('sixth_image', 'default_request_session_value')

slider_value = self.request.POST.get('slider_value')
if step > 9:                                       #This is to stop it entering a blank value into the list
slider_DV_values.insert(step - 7, slider_value)

elif step == 12:
slider_value = self.request.POST.get('slider_value')
if slider_value is not None:
slider_DV_values.insert(step - 7, slider_value)

context['fourth_image'] = self.request.session['fourth_image']
context['fifth_image'] = self.request.session['fifth_image']
context['sixth_image'] = self.request.session['sixth_image']
context['first_slider'] = slider_DV_values[3]
context['second_slider'] = slider_DV_values[4]
context['third_slider'] = slider_DV_values[5]

if step in (13, 14, 15):
image = random.choice(PATH_ONE_IMAGES)
images.insert(step - 6, image)
PATH_ONE_IMAGES.remove(image)
context['display_image'] = image

if step == 13:
self.request.session['seventh_image'] = images[6]
seventh_image = self.request.session.get('seventh_image', 'default_request_session_value')

elif step == 14:
self.request.session['eight_image'] = images[7]
eight_image = self.request.session.get('eight_image', 'default_request_session_value')

elif step == 15:
self.request.session['ninth_image'] = images[8]
ninth_image = self.request.session.get('ninth_image', 'default_request_session_value')

slider_value = self.request.POST.get('slider_value')
if step > 13:                                       #This is to stop it entering a blank value into the list
slider_DV_values.insert(step - 8, slider_value)

elif step == 16:
slider_value = self.request.POST.get('slider_value')
if slider_value is not None:
slider_DV_values.insert(step - 8, slider_value)

context['seventh_image'] = self.request.session['seventh_image']
context['eight_image'] = self.request.session['eight_image']
context['ninth_image'] = self.request.session['ninth_image']
context['first_slider'] = slider_DV_values[6]
context['second_slider'] = slider_DV_values[7]
context['third_slider'] = slider_DV_values[8]

elif step == 17:
context['first_image'] = images[0]
context['second_image'] = images[1]
context['third_image'] = images[2]
context['fourth_image'] = images[3]
context['fifth_image'] = images[4]
context['sixth_image'] = images[5]
context['seventh_image'] = images[6]
context['eight_image'] = images[7]
context['ninth_image'] = images[8]

steps = ['5','6','7','9','10','11','13','14','15']
dv_steps = ['8','12','16',]

context.update({'steps' : steps, 'dv_steps' : dv_steps})

return context

def done(self, form_list, **kwargs):
global SurveyWizardOneCounter
global TotalMaxCounter
SurveyWizardOneCounter += 1
TotalMaxCounter += 1
return render(self.request, 'Return_to_AMT.html', {
'form_data': [form.cleaned_data for form in form_list],
})
• Hi, I saw you this code long time ago. And you still do not descibe problem verbosely. Any way you can move your login into some generator that will return all you need. – outoftime Nov 26 '14 at 19:48
• @outoftime im not really sure what you mean? Yes it is an evolving piece of code I've been working on for a while. I would be happy to explain any part further but I did not want to bore anyone – Deepend Nov 26 '14 at 19:52

if self.steps.current in ['5','6','7','8','9','10','11','12','13','14','15','16', '17', '18']:
step = int(self.steps.current)

This would be shorter and a lot easier this way:

step = int(self.steps.current)
if step in range(5, 19):

Similarly, this could be written differently too:

steps = ['5','6','7','9','10','11','13','14','15']

like this:

steps = [str(x) for x in range(5, 16) if x not in (8, 12)]

Though the benefit of this writing style might be arguable.

These should have be elif too instead of if:

if step in (9, 10, 11):
# ...
if step in (13, 14, 15):
# ...

You have some unused variables, for example in this code:

if step == 5:
self.request.session['first_image'] = images[0]
first_image = self.request.session.get('first_image', 'default_request_session_value')

elif step == 6:
self.request.session['second_image'] = images[1]
second_image = self.request.session.get('second_image', 'default_request_session_value')

elif step == 7:
self.request.session['third_image'] = images[2]
third_image = self.request.session.get('third_image', 'default_request_session_value')

The first_image, second_image, third_image local variables are unused. You can delete those lines. The same goes for the other local variables with similar names too (fourth_image, ..., ninth_image).