djang自定义表单更新

2024-09-29 02:23:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试更新自定义表单,对于user new entry和user update使用相同的表单。在我使用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


Tags: id表单newreturnifrequestjobpost
1条回答
网友
1楼 · 发布于 2024-09-29 02:23:20

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

你是不是想测试一下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':

相关问题 更多 >