如何解决没有名为 django.contrib.auth.hasers 的模块
我正在制作一个用户注册和登录网站,其中我使用的是 Django。我已经执行了 pip install django[argon2]
,但错误 No module named 'django.contrib.auth.hashers.Argon2PasswordHasherdjango'; 'django.contrib.auth.hashers' is not a package
不断出现。
settings.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/'
收到的回溯是
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 950,in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django.contrib.auth.hashers.Argon2PasswordHasherdjango'; 'django.contrib.auth.hashers' is not a package
我还尝试在终端中的同一工作目录中导入 django.contrib.auth.hashers
。它对 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
我还尝试注释掉 Argon2PasswordHasher 并让其他哈希器工作,但它为 PASSWORD_HASHERS
中列出的所有哈希器生成了相同的错误
解决方法
这里需要注意的是
-
django.contrib.auth.hashers
是安装在您的案例中的软件包。 - 但是
Argon2PasswordHasher
是包django.contrib.auth.hashers
内的类
所以你需要像这样导入类
from django.contrib.auth.hashers import Argon2PasswordHasher
您也可以导入整个包并使用其类,如下所示
import django.contrib.auth.hashers as hasher
# using the class inside package
hasher.Argon2PasswordHasher()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。