I am facing an error from this Django project. I have tried all the advice i can come across still its not working properly. Below is the error from the web.
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/login
Django Version: 2.2
Python Version: 3.9.7
Installed Applications:
['main',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\INTROVERTED\Downloads\Compressed\FacialRecognitionForPolice-master\venv1\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\INTROVERTED\Downloads\Compressed\FacialRecognitionForPolice-master\venv1\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\INTROVERTED\Downloads\Compressed\FacialRecognitionForPolice-master\venv1\lib\site-packages\django\core\handlers\base.py" in _get_response
113.`response = wrapped_callback(request, *callback_args, **callback_kwargs)`
File "C:\Users\INTROVERTED\Downloads\Compressed\FacialRecognitionForPolice-master\FacialRecognitionForPolice-master\main\views.py" in login
171.`hashed = bcrypt.hashpw(request.POST['login_password'], bcrypt.gensalt())`
File "C:\Users\INTROVERTED\Downloads\Compressed\FacialRecognitionForPolice-master\venv1\lib\site-packages\bcrypt\__init__.py" in hashpw
79.`raise TypeError("Strings must be encoded before hashing")`
Exception Type: TypeError at /login
Exception Value: Strings must be encoded before hashing
CodePudding user response:
As error message clearly says, you have to encode the password first.
In main/views.py
on line 171 you have
hashed = bcrypt.hashpw(request.POST['login_password'], bcrypt.gensalt())
Your request.POST
data is a string, so you should encode it to bytes:
hashed = bcrypt.hashpw(request.POST['login_password'].encode(), bcrypt.gensalt())
You can select other encoding (not default utf8
) with .encode('ascii')
(choose one and don't change it later, utf8
is probably the best choice in general).