Storing a users submitted jQuery values in a Python/Django view -
below 2 pieces of code (working , broken) aim same thing. unfortunately "working" solution extremely ugly , ungainly, broken code, not much.
as see below, broken code much nicer solution, unfortunately dose not work. main issue have slider not have value until user moves it. default 'none'. hence on step 5 maintain inserting 'none' in list instead of users rating.
if can help me much appreciated.
broken code
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']: 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 slider_value = self.request.post.get('slider_value') slider_dv_values.insert(step - 5, slider_value) elif step == 8: context['first_image'] = images[0] context['second_image'] = images[1] context['third_image'] = images[2] print 'the value of first slider is:', slider_dv_values[0] print 'the value of sec slider is:', slider_dv_values[1] print 'the value of 3rd slider is:', slider_dv_values[2] steps = ['5','6','7','9','10','11','13','14','15'] dv_steps = ['8','12','16'] context.update({'steps': steps, 'dv_steps': dv_steps }) homecoming context
output
the value of first slider is: none
the value of sec slider -66
the value of 3rd slider is: -37
working code
the below code work, , stores right values. dam ugly!
images = [] slider_dv_values = [] 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']: 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 == 6: slider_value = self.request.post.get('slider_value') slider_dv_values.insert(step - 5, slider_value) elif step == 7: slider_value = self.request.post.get('slider_value') slider_dv_values.insert(step - 5, slider_value) elif step == 8: context['first_image'] = images[0] context['second_image'] = images[1] context['third_image'] = images[2] slider_value = self.request.post.get('slider_value') slider_dv_values.insert(step - 5, slider_value) print 'the value of first slider is:', slider_dv_values[0] print 'the value of sec slider is:', slider_dv_values[1] print 'the value of 3rd slider is:', slider_dv_values[2] steps = ['5','6','7','9','10','11','13','14','15'] dv_steps = ['8','12','16'] context.update({'steps': steps, 'dv_steps': dv_steps }) homecoming context
output
the value of first slider is: -99
the value of sec slider -66
the value of 3rd slider is: -37
this should "work":
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: slider_value = self.request.post.get('slider_value') slider_dv_values.insert(step - 5, slider_value)
or this:
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 slider_value = self.request.post.get('slider_value') if slider_value not none: slider_dv_values.insert(step - 5, slider_value)
python django python-2.7
No comments:
Post a Comment