python manage.py createsuperuser在Mac上有效,但在Windows上无效

时间:2019-10-27 18:36:43

标签: django

我已经通过以下链接浏览了Django教程。我已经在Mac上完成此操作,但是在Windows 10上遇到了问题:

djangoproject

在教程第2部分中,当涉及到创建新的超级用户时,它将失败。

如果在Mac上运行以下命令,则可以创建一个新的超级用户。当我在Windows上运行类似的命令时,它将失败。

我尝试在Windows上运行以下两项:

source venv/bin/activate
python manage.py createsuperuser

.\venv\Scripts\activate
py manage.py createsuperuser

包裹(点数列表):

Package     Version
----------- -------
Django      2.2.6
docutils    0.15.2
pip         19.3.1
pycodestyle 2.5.0
pytz        2019.3
setuptools  41.4.0
sqlparse    0.3.0
wheel       0.33.6

存在db.sqlite3数据库,据我所知它具有合理的结构(有很多Django特定表)。

我希望创建一个新的超级用户,但获得以下输出:

Traceback (most recent call last):
  File "C:\Users\Mark\Documents\Development\Python\PYTHON---DJANGO---TUTORIAL\venv\lib\site-packages\django\contrib\auth\password_validation.py", line 26, in get_password_validators
    klass = import_string(validator['NAME'])
  File "C:\Users\Mark\Documents\Development\Python\PYTHON---DJANGO---TUTORIAL\venv\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "C:\Users\Mark\Documents\Development\Python\PYTHON---DJANGO---TUTORIAL\venv\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django.contrib.auth.password_validation                '

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Mark\Documents\Development\Python\PYTHON---DJANGO---TUTORIAL\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Mark\Documents\Development\Python\PYTHON---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 "C:\Users\Mark\Documents\Development\Python\PYTHON---DJANGO---TUTORIAL\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Mark\Documents\Development\Python\PYTHON---DJANGO---TUTORIAL\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 61, in execute
    return super().execute(*args, **options)
  File "C:\Users\Mark\Documents\Development\Python\PYTHON---DJANGO---TUTORIAL\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Mark\Documents\Development\Python\PYTHON---DJANGO---TUTORIAL\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 132, in handle
    validate_password(password2, self.UserModel(**fake_user_data))
  File "C:\Users\Mark\Documents\Development\Python\PYTHON---DJANGO---TUTORIAL\venv\lib\site-packages\django\contrib\auth\password_validation.py", line 44, in validate_password
    password_validators = get_default_password_validators()
  File "C:\Users\Mark\Documents\Development\Python\PYTHON---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 "C:\Users\Mark\Documents\Development\Python\PYTHON---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.

1 个答案:

答案 0 :(得分:0)

您怀疑problem is in your password validation configuration是用反斜杠设置这些行的延续方式。

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

评估为

[
{'NAME': 'django.contrib.auth.password_validation                .UserAttributeSimilarityValidator'}, 
{'NAME': 'django.contrib.auth.password_validation                .MinimumLengthValidator'}, 
{'NAME': 'django.contrib.auth.password_validation                .CommonPasswordValidator'}, 
{'NAME': 'django.contrib.auth.password_validation                .NumericPasswordValidator'}
]

,没有django/contrib/auth/password_validation .py这样的模块。

如果相同的代码可以在macOS上运行,那么...好吧,很奇怪,这可能是文件名规范化的事情,但这仍然是问题所在。

解决方法自然是将其更改为

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

或者如果您希望这些行继续,

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation."
        "UserAttributeSimilarityValidator"
    },
    {
        "NAME": "django.contrib.auth.password_validation."
        "MinimumLengthValidator"
    },
    {
        "NAME": "django.contrib.auth.password_validation."
        "CommonPasswordValidator"
    },
    {
        "NAME": "django.contrib.auth.password_validation."
        "NumericPasswordValidator"
    },
]

–即字符串文字背靠背,隐式地将它们串联在一起–即使我认为可读性较差,也同样有效。