EInherjar
EInherjar

Reputation: 339

Error when creating admin in django tutorial

I am following the Django tutorial and am stuck on the second part, where I have to create an admin account.

I followed everything described in the tutorial up to that point and get the following error:

Traceback (most recent call last):
  File "e:\venvs\django_tutorial_venv\lib\site-packages\django\utils\module_loading.py", line 20, in import_string
    return getattr(module, class_name)
AttributeError: module 'django.contrib.auth.password_validation' has no attribute '        UserAttributeSimilarityValidator'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "e:\venvs\django_tutorial_venv\lib\site-packages\django\contrib\auth\password_validation.py", line 26, in get_password_validators
    klass = import_string(validator['NAME'])
  File "e:\venvs\django_tutorial_venv\lib\site-packages\django\utils\module_loading.py", line 24, in import_string
    ) from err
ImportError: Module "django.contrib.auth.password_validation" does not define a "        UserAttributeSimilarityValidator" attribute/class

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   
    File "manage.py", line 15, in <module>
      execute_from_command_line(sys.argv)   
    File "e:\venvs\django_tutorial_venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
      utility.execute()   
    File "e:\venvs\django_tutorial_venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
      self.fetch_command(subcommand).run_from_argv(self.argv)   
    File "e:\venvs\django_tutorial_venv\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
      self.execute(*args, **cmd_options)   
    File "e:\venvs\django_tutorial_venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 59, in execute
      return super().execute(*args, **options)   
    File "e:\venvs\django_tutorial_venv\lib\site-packages\django\core\management\base.py", line 353, in execute
      output = self.handle(*args, **options)   
    File "e:\venvs\django_tutorial_venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 163, in handle
      validate_password(password2, self.UserModel(**fake_user_data))
    File "e:\venvs\django_tutorial_venv\lib\site-packages\django\contrib\auth\password_validation.py", line 44, in validate_password
      password_validators = get_default_password_validators()
    File "e:\venvs\django_tutorial_venv\lib\site-packages\django\contrib\auth\password_validation.py", line 19, in get_default_password_validators
      return get_password_validators(settings.AUTH_PASSWORD_VALIDATORS)           
    File "e:\venvs\django_tutorial_venv\lib\site-packages\django\contrib\auth\password_validation.py", line 29, in get_password_validators
      raise ImproperlyConfigured(msg % validator['NAME']) 
django.core.exceptions.ImproperlyConfigured: The module in NAME could not be imported: django.contrib.auth.password_validation.        UserAttributeSimilarityValidator. Check your AUTH_PASSWORD_VALIDATORS setting.

One difference that I made was, that I put my virtualenvs in a folder, different than my project folder; could that be an issue? I correctly point to the venv folder in my project. What exactly is the error here?

Upvotes: 3

Views: 5295

Answers (4)

sudhanshu
sudhanshu

Reputation: 453

Following line of code in settings resolved the issue.

AUTH_PWD_MODULE="django.contrib.auth.password_validation."

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": f"{AUTH_PWD_MODULE}UserAttributeSimilarityValidator",
    },
    {
        "NAME": f"{AUTH_PWD_MODULE}MinimumLengthValidator",
    },
    {
        "NAME": f"{AUTH_PWD_MODULE}CommonPasswordValidator",
    },
    {
        "NAME": f"{AUTH_PWD_MODULE}NumericPasswordValidator",
    },
]

I found it more readable. The actual issue is when try format string using \, the name is changed.

Upvotes: 3

DeshErBojhaa
DeshErBojhaa

Reputation: 486

To add more detailed answer on @Elnherjar 's answer, on your settings.py don't do

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.\
            UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.\
            MinimumLengthValidator',
    },
   ...
]

which appends a tab in those strings. A cleaner approach can be,

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.' +
        'UserAttributeSimilarityValidator',
    },
...
]

Upvotes: 9

EInherjar
EInherjar

Reputation: 339

Okay, after some digging around, I found out that the solution was fairly simple. The line

File "e:\venvs\django_tutorial_venv\lib\site-packages\django\contrib\auth\password_validation.py", line 29, in get_password_validators
      raise ImproperlyConfigured(msg % validator['NAME']) 
django.core.exceptions.ImproperlyConfigured: The module in NAME could not be imported: django.contrib.auth.password_validation.        UserAttributeSimilarityValidator. Check your AUTH_PASSWORD_VALIDATORS setting.

is faulty, specifically, I conformed to the pep8 standard and I mistakenly added spaces in front of UserAttributeSimilarityValidator, so it read the full string with spaces included, that's why it couldn't find the package. I corrected the strings, ignoring pep8 and it worked.

Upvotes: 4

slqq
slqq

Reputation: 265

following your traceback you might need to Look at your settings and look for AUTH_PASSWORD_VALIDATORS, there may be some syntax error

Upvotes: 1

Related Questions