Note that there are some explanatory texts on larger screens.

plurals
  1. POstring index out of range Python, Django
    primarykey
    data
    text
    <p>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. </p> <p>Here is my Django View: </p> <pre><code>def submitworkout(request): #workoutinfo = workout(request.GET) return render_to_response('home.html',{'infoprompt': workout(request.GET)},context_instance=RequestContext(request)) </code></pre> <p>Here is the object: </p> <pre><code>class workout(): def __init__(self,addworkout): self.workout = collections.OrderedDict(); getallreps = 0 for i in range(len(addworkout['exercisename'])): numsetcounter = 0; self.workout[string.capwords(addworkout['exercisename'][i])] = [] while numsetcounter &lt; 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 +=1 numsetcounter +=1 def getexercise(self,name): try: return self.workout[string.capwords(name)]; except: return 'This exercise does not exist!' </code></pre> <p>Now this is the dictionary i'm trying to run through the class: </p> <pre><code>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']} </code></pre> <p>and here are the local vars that Django is displaying on the error: </p> <pre><code>i=1 numsetcounter =0 getallreps = 1 </code></pre> <p>Hopefull you guys help me resolve my problem. Thanks in advance! </p> <p>EDIT: Traceback</p> <pre><code>Environment: Request Method: GET Request URL: http://localhost:8000/submitworkout/?exercisename=Squat&amp;numsets=1&amp;weightinputboxes=195&amp;repinputboxes=5&amp;exercisename=Power+Clean&amp;numsets=1&amp;weightinputboxes=170&amp;repinputboxes=3 Django 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_response 111. response = callback(request, *callback_args, **callback_kwargs) File "C:\Users\Chris\testdjango\fitness\views.py" in submitworkout 34. 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 &lt; int(addworkout['numsets'][i]): # u'numsets': [u'1', u'2'] Exception Type: IndexError at /submitworkout/ Exception Value: string index out of range </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload