0

My project is working fine on my local machine but when I deploy it to my server I am getting an error

Exception Value: list index out of range

Exception Location: /var/www/bias_experiment/src/survey/views.py in get_context_data, line 151

As I said the project is working fine on my local machine. When I occasionally get this error I simply re-sync the DB which has always fixed it.

I have tried a number of things:

  • Re-synced the DB on the server python manage.py syncdb
  • Restarted my server sudo service apache2 restart

This had no effect so I

  • Deleted my deployed project entirely sudo rm -rf my_project
  • Deleted the old DB DROP DATABASE my_db_name
  • Uploaded my project again
  • Created a new DB CREATE DATABASE my_db_name
  • Synced it with the project 'python manage.py syncdb`
  • Ran collectstatic python manage.py collectstatic
  • Ran a2ensite
  • Restarted my server sudo service apache2 restart

However I am still getting the same error. I have no idea why the code works locally but breaks on my production server.

Any ideas? Thanks in advance

  • Local machine: Python 2.7.5, Django 1.6.2
  • Server: Ubuntu 12.4, Apache 2.2.22, Python 2.7.3, Django 1.6

EDIT: This is the offending code from views.py. Lines 128 - 156

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']:
            print '\nThe available list of Path_One images is', PATH_ONE_IMAGES              
            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')
                if slider_value is not None:
                    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'] = images[0]
                context['second_image'] = images[1]
                context['third_image'] = images[2]                  
                context['first_slider'] = slider_DV_values[0] 
                context['second_slider'] = slider_DV_values[1]      
                context['third_slider'] = slider_DV_values[2]                

            elif 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 

Thanks

Deepend
  • 4,057
  • 17
  • 60
  • 101
  • This happened to me with a Heroku deployment as well. Could you paste line 151 of your views.py? – Ian Price Nov 22 '14 at 18:47
  • Done. I added the section which is causing the issue. Line 151 is `context['first_image'] = images[0]` – Deepend Nov 22 '14 at 18:52
  • Well, `images` is being updated with values when the current step is 5,6, or 7 but the values are being taken out when the step is 8. I'd be very cautious about about assuming the FormWizard instance is persistent. Just a hunch – Ian Price Nov 22 '14 at 19:04
  • So `images[0]` is empty when the user gets to step 8. I am a bit of a beginner programmer, completely self thought (and with a lot of help from here) Any ideas on how to either persist the data to step 8 or make a FormWizard persistent? – Deepend Nov 22 '14 at 19:09
  • 1
    You'd either want to store the list in the session object at step 5/6/7 and pull it from the session at step 8, or create the images at step 8 by pulling the submitted data from previous with `self.get_cleaned_data_for_step('5')`. – Ian Price Nov 22 '14 at 19:13
  • It also does not explain why it works on my local machine but on on the deployed version. – Deepend Nov 22 '14 at 19:13
  • 1
    Well, there's a few reasons that could be; if you have multiple Django instances (i.e. 2+ dynos in the case of Heroku) you may be loading the formwizard at step 8 from a different dyno that step 7, at which point there is no `images` (or an empty instantiation of a list). Or, multiple Gunicorn workers may be running separate Django instances. When using `runserver` you are only using one worker, one process, and it won't reset automatically (like some production servers may if an error is encountered). – Ian Price Nov 22 '14 at 19:17
  • Whats confusing me is that I thought the very idea of the 'get_context_data' method` was to persist data so it could be used on a later page? – Deepend Nov 22 '14 at 19:27
  • get_context_data determines what context variables are passed to the template at each step. Variables created at one step will not be available at another (necessarily) even if you add them as attributes of the wizard itself. – Ian Price Nov 22 '14 at 19:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65440/discussion-between-deepend-and-ian-price). – Deepend Nov 22 '14 at 19:35
  • The FormWizard relies on POST data to take a request and determine what step it is at, previous answers, etc. With a SessionWizardView you have a consistent session and could store variables in it (perhaps using django-picklefield). Otherwise I would never expect a variable defined in one step to be available at a later step in a production environment. – Ian Price Nov 22 '14 at 19:36

0 Answers0