Python/Django: Issues assigning values to a list -
i trying assign output of random.choice(path_one_images)
list , subsequently remove path_one_images
can no longer selected. meant returned along other variables user via get_context_data
method, part of djangos sesionwizardview.
i don't think i'm attempting hard have met few issues.
views.py
path_one_images = ['p1d1.jpg', 'p2d2.jpg', 'p3d3.jpg', 'p4d4.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']: images = [] step = int(self.steps.current) if step in (5, 6, 7): images[step - 5] = image = random.choice(path_one_images) path_one_images.remove(image) context['display_image'] = image elif step == 8: context['first_image'] = images[0] context['second_image'] = images[1] context['third_image'] = images[2] steps = ['5','6','7','9','10','11','13','14','15'] context.update({'steps': steps}) homecoming context
you can see in above when user gets step 5 list images, @ position 0 (5-5) called image , takes value of random.choice(path_one_images)
. removed original list , display_image, shown user, takes value of image. when user gets step 8 shown 3 images again. hope explains i'm trying do.
all of isses seem line
images[step - 5] = image = random.choice(path_one_images)
in above version error
exception value: list assignment index out of range
based on other questions have tried
images.append[step - 5] = image = random.choice(path_one_images) images.insert[step - 5] = image = random.choice(path_one_images)
but both give me error
exception value: 'builtin_function_or_method' object not back upwards item assignment
does know i'm doing wrong?
update ---------------------------------------------------------------
if step in (5, 6, 7): image = random.choice(path_one_images) images.insert(step - 5, image) #=============================================================== # image = random.choice(path_one_images) # images.append(image) #=============================================================== path_one_images.remove(image) context['display_image'] = image elif step == 8: context['first_image'] = images[0] context['second_image'] = images[1] context['third_image'] = images[2]
using either insert or append results in same error @ context['first_image'] = images[0]
exception value: list index out of range
images.append[step - 5] = image images.insert[step - 5] = image
are syntactically incorrect.
append()
, insert()
functions. utilize them (with parenthesis instead of brackets):
images.append(image) images.insert(step - 5, image)
django python-2.7
No comments:
Post a Comment