使用 .encode('utf-8') 并且仍然得到 Unicode-objects must be encoded before hashing 错误

Using .encode('utf-8') and still getting an Unicode-objects must be encoded before hashing error

当我搜索错误时,所有答案都指向使用 .encode('utf-8')。我将其添加到我的代码中,但仍然出现相同的错误。这是我的代码

def login(request):
    password=request.POST['password']
    email=request.POST['email']

    try:
        d2 = Register.objects.latest('created_at')
        registered = Register.objects.get(email = email)
    except:
        return HttpResponse(Register.userManager.not_found_email())

    pw_bytes = password.encode('utf-8')
    hashed = bcrypt.hashpw(pw_bytes, registered.salt)
    print hashed

    if hashed:
        print hashed
        print registered.password
        return HttpResponse(Register.userManager.incorrect_password())

    return render(request, 'validation_app/success.html')

registered.salt bcrypt.gensalt() 保存到我的数据库

只要将字节保存到数据库中的文本字段,它们就会变成文本,因此您也需要对 salt 进行编码。