简体   繁体   English

没有名为 django.contrib.auth.hashers 的模块

[英]No module named django.contrib.auth.hashers

I am making a User Registration and Login website, wherein I am using Django.我正在制作一个用户注册和登录网站,其中我使用的是 Django。 I have already executed pip install django[argon2] but the error No module named 'django.contrib.auth.hashers.Argon2PasswordHasherdjango'; 'django.contrib.auth.hashers' is not a package我已经执行pip install django[argon2]但是错误No module named 'django.contrib.auth.hashers.Argon2PasswordHasherdjango'; 'django.contrib.auth.hashers' is not a package No module named 'django.contrib.auth.hashers.Argon2PasswordHasherdjango'; 'django.contrib.auth.hashers' is not a package keeps on coming. No module named 'django.contrib.auth.hashers.Argon2PasswordHasherdjango'; 'django.contrib.auth.hashers' is not a package

settings.py设置.py

    """
Django settings for User_Authentication project.

Generated by 'django-admin startproject' using Django 3.1.7.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIR  = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")
MEDIA_DIR = os.path.join(BASE_DIR, "media")


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ')o5ax4nab1hip&ef6z^g_19if25lexbpzf=vks&6s#lwi!wx_('

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'basic_app'
]

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',
]

ROOT_URLCONF = 'User_Authentication.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR,],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'User_Authentication.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

PASSWORD_HASHERS = [
    'django.contrib.auth.hashers.Argon2PasswordHasher'
    'django.contrib.auth.hashers.BCryptSHA256PasswordHasher'
    'django.contrib.auth.hashers.BCryptPasswordHasher'
    'django.contrib.auth.hashers.PBKDF2PasswordHasher'
    'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
]

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


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    STATIC_DIR,
]

# Media
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'

The traceback received was收到的回溯是

    Traceback (most recent call last):
  File "/home/swati/Documents/Django_Tutorial/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/swati/Documents/Django_Tutorial/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/swati/Documents/Django_Tutorial/User_Authentication/basic_app/views.py", line 22, in register
    user.set_password(user.password)        # Hashing the passwords -> Encrypting them
  File "/home/swati/Documents/Django_Tutorial/venv/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 99, in set_password
    self.password = make_password(raw_password)
  File "/home/swati/Documents/Django_Tutorial/venv/lib/python3.6/site-packages/django/contrib/auth/hashers.py", line 80, in make_password
    hasher = get_hasher(hasher)
  File "/home/swati/Documents/Django_Tutorial/venv/lib/python3.6/site-packages/django/contrib/auth/hashers.py", line 121, in get_hasher
    return get_hashers()[0]
  File "/home/swati/Documents/Django_Tutorial/venv/lib/python3.6/site-packages/django/contrib/auth/hashers.py", line 89, in get_hashers
    hasher_cls = import_string(hasher_path)
  File "/home/swati/Documents/Django_Tutorial/venv/lib/python3.6/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django.contrib.auth.hashers.Argon2PasswordHasherdjango'; 'django.contrib.auth.hashers' is not a package

I have also tried to import django.contrib.auth.hashers in the terminal in the same working directory.我还尝试在同一工作目录的终端中导入django.contrib.auth.hashers It imports fine for django.contrib.auth.hashers , but as soon as django.contrib.auth.hashers.Argon2PasswordHasher is imported, it generates它可以很好地导入django.contrib.auth.hashers ,但只要django.contrib.auth.hashers.Argon2PasswordHasher被导入,它就会生成

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'django.contrib.auth.hashers.Argon2PasswordHasher'; 'django.contrib.auth.hashers' is not a package

I also tried commenting out the Argon2PasswordHasher and letting other hashers work, but it generated the same error for all hashers listed in PASSWORD_HASHERS我还尝试注释掉 Argon2PasswordHasher 并让其他哈希器工作,但它对PASSWORD_HASHERS中列出的所有哈希器产生了相同的错误

Things to note here is这里要注意的是

  • django.contrib.auth.hashers is a package which is installed in your case. django.contrib.auth.hashers是安装在您的机箱中的 package。
  • But Argon2PasswordHasher is class inside package django.contrib.auth.hashers但是Argon2PasswordHasher是 package django.contrib.auth.hashers 内的django.contrib.auth.hashers

so u need to import the class like所以你需要像这样导入 class

from django.contrib.auth.hashers import Argon2PasswordHasher

Also u can import entire package and use its classes, as shown below您也可以导入整个 package 并使用其类,如下所示

import django.contrib.auth.hashers as hasher  

# using the class inside package
hasher.Argon2PasswordHasher()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DJANGO:没有名为 social_auth.backends.contrib.github 的模块 - DJANGO: No module named social_auth.backends.contrib.github 扩展django.contrib.auth模块 - Extending django.contrib.auth module 没有名为“django.contrib.staticfiles.templatetags”的模块 - No module named 'django.contrib.staticfiles.templatetags' ModuleNotFoundError:没有名为“django.contrib.staticfilesbase”的模块 - ModuleNotFoundError: No module named 'django.contrib.staticfilesbase' 带有Django的Pyinstaller:ModuleNotFoundError:没有名为django.contrib.messages.apps的模块 - Pyinstaller with Django: ModuleNotFoundError: No Module Named django.contrib.messages.apps Django - 不正确的配置:模块“django.contrib.auth.middleware” - Django - ImproperlyConfigured: Module “django.contrib.auth.middleware” 带有Django的Pyinstaller:ModuleNotFoundError:没有名为“ django.contrib.admin.apps”的模块 - Pyinstaller with Django: ModuleNotFoundError: No module named 'django.contrib.admin.apps' DJANGO CRUD 没有名为“django.contrib.name”的模块 - DJANGO CRUD No module named 'django.contrib.name' AttributeError:模块“ django.contrib.auth.views”没有属性“ LoginView” - AttributeError: module 'django.contrib.auth.views' has no attribute 'LoginView' AttributeError:模块&#39;django.contrib.auth.views&#39;没有属性&#39;login&#39; - AttributeError: module 'django.contrib.auth.views' has no attribute 'login'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM