1

カスタム フォームを更新しようとしました。ユーザーの新しいエントリとユーザーの更新には、同じフォームが使用されます。送信コードで if else を更新と送信に使用すると、「文字列インデックスは 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')

このコードを実行すると、「文字列インデックスは 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'

あなたが彼らがしていると思うことをしないでください。おそらく、 が 2 番目のブロックと等しいかどうかをテストしたいと思うでしょう。usubmit'Update'

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