Reputation: 794
i try to update the custom form, for user new entry and user update the same form is used. in submit code i use if else for update and submit, it show an error " string indices must be integers, not str "
views.py:-
def applicationvalue(request):
if request.method == 'POST':
if request.method['usubmit'] == 'new':
getappid = request.POST['appid']
getjobtitle = request.POST['jobtitle']
getodesk = request.POST['odeskid']
getspecification = request.POST['clientspecification']
getnotes = request.POST['notes']
request.session['getappid'] = getappid
getintable = applicationform(user_id = request.user.id , app_id = getappid, job_title = getjobtitle, odesk_id = getodesk, client_specification = getspecification, job_type = request.POST['jobtype'], notes = getnotes)
getintable.save()
return HttpResponseRedirect('/tableview/')
else:
request.method['usubmit'] == 'Update'
saveapplid = request.POST['appid']
savejobtitle = request.POST['jobtitle']
saveodesk = request.POST['odeskid']
savespecification = request.POST['clientspecification']
savenotes = request.POST['notes']
saveapp = applicationform.objects.get(app_id = saveapplid)
saveapp.job_title = savejobtitle
saveapp.odesk_id = saveodesk
saveapp.user_specification = savespecification
saveapp.notes = savenotes
saveapp.save()
return HttpResponse(1)
# return HttpResponseRedirect('/tableview/')
else:
return render_to_response('registration/applicationform.html')
when this code run than it display an error " string indices must be integers, not str "
Upvotes: 1
Views: 94
Reputation: 1122342
request.method
is a string (you just tested if it is equal to "POST"
in the first if
statement)!
Did you meant to test against request.POST['usubmit']
instead?
The line:
if request.method['usubmit'] == 'new':
will throw an error, but perhaps you wanted:
if request.POST['usubmit'] == 'new':
instead. Moreover, the lines:
else:
request.method['usubmit'] == 'Update'
don't do what you think they do. You probably wanted to test if usubmit
is equal to 'Update'
for the second block:
elif request.POST['usubmit'] == 'Update':
Upvotes: 2