1

I am trying to add custom password validators in Django but i can't figure out why it wouldn't import the module. Anyone has encountered this issue or can tell me why i have this error please ?

Here is the code in authentication/validators.py:

from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _

class LowercaseValidator(object):
    def validate(self, password, user=None):
        if not re.findall('[a-z]', password):
            raise ValidationError(
                _("The password must contain at least 1 lowercase letter, a-z."),
                code='password_no_lower',
            )

    def get_help_text(self):
        return _(
            "Your password must contain at least 1 lowercase letter, a-z."
        )

Here is my updated settings.py

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        'OPTIONS': {
            'min_length': 12, }
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
    {
        'NAME': 'authentication.validators.LowercaseValidator',
    },
]

And here is the error i have:

The module in NAME could not be imported: authentication.validators.LowercaseValidator. Check your AUTH_PASSWORD_VALIDATORS setting.

P.S: i did create an app called "authentication" and my validators.py is in the folder of the application.

Thank you in advance for your answers !

akamob
  • 23
  • 4
  • Can you try importing the validator in a shell and see what the full import error is? Start a shell with `python manage.py shell` and then run `from django.utils.module_loading import import_string; import_string('authentication.validators.LowercaseValidator')` – Iain Shelvington Jul 18 '22 at 23:35
  • Hi, Tanks for your answer. Here is the error i get >ImportError: cannot import name 'ugettext' from 'django.utils.translation' (.../backend/.venv/lib/python3.9/site-packages/django/utils/translation/__init__.py) – akamob Jul 19 '22 at 10:01

0 Answers0