i'm using Django to develop a web application. When i try and run it on my web form i am receiving 'string index out of range' error. However, when i hardcode a dictionary in to a python test file it works fine with the same values.
Here is my Django View:
def submitworkout(request):#workoutinfo = workout(request.GET)return render_to_response('home.html',{'infoprompt': workout(request.GET)},context_instance=RequestContext(request))
Here is the object:
class workout():def __init__(self,addworkout):self.workout = collections.OrderedDict();getallreps = 0for i in range(len(addworkout['exercisename'])): numsetcounter = 0; self.workout[string.capwords(addworkout['exercisename'][i])] = [] while numsetcounter < int(addworkout['numsets'][i]): # print self.workout[addworkout['exercisename'][i]]self.workout[string.capwords(addworkout['exercisename'][i])].append([addworkout['weightinputboxes'][getallreps],addworkout['repinputboxes'][getallreps]]) #[getallreps +=1numsetcounter +=1 def getexercise(self,name): try: return self.workout[string.capwords(name)];except:return 'This exercise does not exist!'
Now this is the dictionary i'm trying to run through the class:
addworkout = {u'repinputboxes': [u'5', u'3'], u'weightinputboxes': [u'195', u'170'], u'numsets': [u'1', u'1'], u'exercisename': [u'Squat', u'Power Clean']}
and here are the local vars that Django is displaying on the error:
i=1 numsetcounter =0getallreps = 1
Hopefull you guys help me resolve my problem. Thanks in advance!
EDIT: Traceback
Environment:Request Method: GET
Request URL: http://localhost:8000/submitworkout/?exercisename=Squat&numsets=1&weightinputboxes=195&repinputboxes=5&exercisename=Power+Clean&numsets=1&weightinputboxes=170&repinputboxes=3Django Version: 1.3.1
Python Version: 2.7.0
Installed Applications:
('django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.sites','django.contrib.messages','django.contrib.staticfiles','django.contrib.admin','authentication')
Installed Middleware:
('django.middleware.common.CommonMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware')Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Chris\testdjango\fitness\views.py" in submitworkout34. return render_to_response('home.html',{'infoprompt': workout(request.GET)},context_instance=RequestContext(request))
File "C:\Users\Chris\testdjango\fitness\tracking\models.py" in __init__15. while numsetcounter < int(addworkout['numsets'][i]): # u'numsets': [u'1', u'2']Exception Type: IndexError at /submitworkout/
Exception Value: string index out of range