【问题标题】:Error django " MultiValueDictKeyError at /employeeUpdate/ 'id' " affter I edit data编辑数据后出现错误 django“MultiValueDictKeyError at /employee Update/'id'”
【发布时间】:2021-03-28 10:08:16
【问题描述】:

在我编辑数据后出现错误 django “MultiValueDictKeyError at /employeeUpdate/'id'”。 这是我在views.py中的代码

def employeeUpdate(request):
    id = request.POST['id']
    emp_username = request.POST['emp_username']
    emp_password = request.POST['emp_password']
    emp_identification_code = request.POST['emp_identification_code']
    content = employee.objects.get(pk = id)
    content.emp_username = emp_username
    content.emp_password = emp_password
    content.emp_identification_code = emp_identification_code

    if(employee.objects.filter(emp_username=emp_username)):
        messages.success(request, 'Username already exists')
        return redirect("/employeeUpdate")
    elif(employee.objects.filter(emp_identification_code=emp_identification_code)):
        messages.success(request, 'Identification already exists')
        return redirect("/employeeUpdate")
    else:
        content.save()
        messages.success(request, 'Update already !!!')
        return redirect("/employeeBoard")

【问题讨论】:

    标签: python html css django web-applications


    【解决方案1】:

    您正在尝试访问request.POST['id'],这不是不存在的。如果字典不存在,您可以尝试从字典中访问默认值

    id =request.POST.get('id', default_value)
    

    如果你想访问当前用户的id,那么你可以试试

    id = request.user.id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 2019-07-15
      相关资源
      最近更新 更多