List Index out of Range

17 views
Skip to first unread message

Eugene TUYIZERE

unread,
Oct 2, 2021, 11:07:13 AM10/2/21
to django...@googlegroups.com
Good day all,

I need assistance. Am trying to save data from the below form but I am getting list out of range error
 can someone help me to write a successful save function?

here is the code I am using:


def submit_work(request):
if request.method != "POST":
return HttpResponse("<h2>Method Not Allowed</h2>")
else:
pillar_id=request.POST.get("pillar")
object_id=request.POST.get("objects")
output_id=request.POST.get("output")
activity_id=request.POST.get("activity")
input_list=request.POST.getlist("input[]")
time_frame_list=request.POST.getlist("timeframe[]")
responsible_list=request.POST.getlist("responsible[]")
j=0
for inp in input_list:
wf=WORKFRAMS(pillar_id=pillar_id,object_id=object_id,output_id=output_id,activity_id=activity_id,
input=inp,time_frame=time_frame_list[j],responsible=responsible_list[j])
wf.save()
j=j+1
return HttpResponse("OK")



image.png

Regards, 

TUYIZERE Eugene


bnmng

unread,
Oct 2, 2021, 8:13:35 PM10/2/21
to Django users
Which line in your code is triggering the error?

Hedrick Godson's

unread,
Oct 2, 2021, 8:21:18 PM10/2/21
to django...@googlegroups.com
Make sure you fill all the inputs with list and also make sure that you provide the correct input name to request.POST.getlist() method otherwise it will returns empty list which will raise IndexError

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8d69a634-0a2d-42e3-994c-5e19f88a799en%40googlegroups.com.

Lalit Suthar

unread,
Oct 3, 2021, 3:00:55 AM10/3/21
to django...@googlegroups.com
Adding to above 2 answers index error may also occur when your time_frame_list or responsible_list does not have the same number of items as your input_list. You should try bnmng' s answer also 

> Which line in your code is triggering the error?

you check it with simple print statements or using a debugger in vscode, or with ipdb.set_trace()


Reply all
Reply to author
Forward
0 new messages