1

我尝试更新自定义表单,对于用户新条目和用户更新,使用相同的表单。在提交代码中,我使用 if else 进行更新和提交,它显示错误“字符串索引必须是整数,而不是 str”

视图.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')

当此代码运行时,它显示错误“字符串索引必须是整数,而不是 str”

4

1 回答 1

2

request.method"POST"是一个字符串(你刚刚在第一条语句中测试了它是否等于if)!

您是否打算进行测试request.POST['usubmit']

该行:

if request.method['usubmit'] == 'new':

会抛出一个错误,但也许你想要:

if request.POST['usubmit'] == 'new':

反而。此外,这些行:

else:
    request.method['usubmit'] == 'Update'

不要做你认为他们做的事。您可能想测试第二个块是否usubmit等于'Update'

elif request.POST['usubmit'] == 'Update':
于 2013-09-13T08:38:46.920 回答