This follows on from a previous question I asked which was of huge help to me. I am still only learning Python and Django and I would love to learn how to code better.
The below is a get_context_data
method which is used to return a variable to an internal page of a Django SessionWizardView
. The SessionWizardView splits a lengthy form over multiple pages.
The below is designed to show the user a different image, selected at random from path_one_images
to the user. After the user views three such images they are then presented with the previous three on the forth page. This is repeated until all 9 images are seen by the user.
In the previous question I asked shortening the code proved to be a simple matter as each of my if
statements were essentially doing the same thing. However In this case they are further nested, undertake different (but similar) functions and are interrupted by the multi views.
Has anyone got any suggestions for optimising, and preferably condensing the below code? Remember I am still learning so any suggestion would be helpful.
views.py
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']:
print '\nThe available list of Path_One images is', path_one_images
global first_image, second_image, third_image, fourth_image, fifth_image, sixth_image, seventh_image, eight_image, ninth_image
if self.steps.current == '5':
first_image = random.choice(path_one_images)
path_one_images.remove(first_image)
display_image = first_image
context['display_image'] = display_image
if self.steps.current == '6':
second_image = random.choice(path_one_images)
path_one_images.remove(second_image)
display_image = second_image
context['display_image'] = display_image
if self.steps.current == '7':
third_image = random.choice(path_one_images)
path_one_images.remove(third_image)
display_image = third_image
context['display_image'] = display_image
if self.steps.current == '8':
context['first_image'] = first_image
context['second_image'] = second_image
context['third_image'] = third_image
if self.steps.current == '9':
fourth_image = random.choice(path_one_images)
path_one_images.remove(fourth_image)
display_image = fourth_image
context['display_image'] = display_image
if self.steps.current == '10':
fifth_image = random.choice(path_one_images)
path_one_images.remove(fifth_image)
display_image = fifth_image
context['display_image'] = display_image
if self.steps.current == '11':
sixth_image = random.choice(path_one_images)
path_one_images.remove(sixth_image)
display_image = sixth_image
context['display_image'] = display_image
if self.steps.current == '12':
context['fourth_image'] = fourth_image
context['fifth_image'] = fifth_image
context['sixth_image'] = sixth_image
if self.steps.current == '13':
seventh_image = random.choice(path_one_images)
path_one_images.remove(seventh_image)
display_image = seventh_image
context['display_image'] = display_image
if self.steps.current == '14':
eight_image = random.choice(path_one_images)
path_one_images.remove(eight_image)
display_image = eight_image
context['display_image'] = display_image
if self.steps.current == '15':
ninth_image = random.choice(path_one_images)
path_one_images.remove(ninth_image)
display_image = ninth_image
context['display_image'] = display_image
if self.steps.current == '16':
context['seventh_image'] = seventh_image
context['eight_image'] = eight_image
context['ninth_image'] = ninth_image
steps = ['5','6','7','9','10','11','13','14','15']
context.update({'steps': steps})
return context