我正在使用 Django 开发 Web 应用程序。当我尝试在我的网络表单上运行它时,我收到“字符串索引超出范围”错误。但是,当我将字典硬编码到 python 测试文件中时,它可以在相同的值下正常工作。
这是我的 Django 视图:
def submitworkout(request):
#workoutinfo = workout(request.GET)
return render_to_response('home.html',{'infoprompt': workout(request.GET)},context_instance=RequestContext(request))
这是对象:
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 < 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!'
现在这是我试图通过类运行的字典:
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']}
以下是 Django 在错误中显示的本地变量:
i=1
numsetcounter =0
getallreps = 1
希望你们帮我解决我的问题。提前致谢!
编辑:追溯
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=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 < int(addworkout['numsets'][i]): # u'numsets': [u'1', u'2']
Exception Type: IndexError at /submitworkout/
Exception Value: string index out of range