Bcrypt return TypeError Unicode-объекты должны быть закодированы перед проверкой [закрыто]
Я пытаюсь создать логин и регистрацию с помощью django. Вся база данных хранится в MySql.
def signup(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
if username and password:
Myobject = adminUser()
Myobject.username = username
Myobject.password = bcrypt.hashpw(password.encode('utf8'),bcrypt.gensalt())
Myobject.save()
print("User Created Successfully")
else:
messages.WARNING("Username and Password can not be blank")
return render(request,'signup.html')
Используя эту функцию регистрации, я могу сохранить имя пользователя и хэшированный пароль в базе данных.
def login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
if username is not None and password is not None:
user=adminUser.objects.get(username=username)
hashed_password = user.password
is_check = bcrypt.checkpw(password.encode('utf8'),hashed_password)
print(is_check)
return render(request,'login.html')
Во время входа в систему я получаю ошибку TypeError. Я уже закодировал пароль.
Это ошибка ->
Internal Server Error: /admin_users/login
Traceback (most recent call last):
File "C:\Users\v_kum\Documents\myenv\lib\site-
packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request)
File "C:\Users\v_kum\Documents\myenv\lib\site-
packages\django\core\handlers\base.py", line 181, in _get_response response
= wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\v_kum\Documents\My Project\python_test\app_kanri\admin_users.py", line 18, in login
is_check = bcrypt.checkpw(password.encode('utf8'),hashed_password)
File "C:\Users\v_kum\Documents\myenv\lib\site-packages\bcrypt\__init__.py", line 120, in checkpw raise TypeError("Unicode-objects must be encoded before checking")
TypeError: Unicode-objects must be encoded before checking