Skip to main content
 首页 » 编程设计

python之如何在 django-allauth 中设置用户密码的最小字符数

2024年08月21日35pander-it

我想更改 django-allauth 中的 ACCOUNT_PASSWORD_MIN_LENGTH。我在 settings.py 中尝试了 ACCOUNT_PASSWORD_MIN_LENGTH = 5 但它不起作用,Django 仍然说我需要输入 8 个字符,就像默认情况一样。

这是我的 settings.py 文件:

# Application definition 
 
INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'blog', 
    'taggit', 
    'django_summernote', 
    'django.contrib.sites', 
    'allauth', 
    'allauth.account', 
    'allauth.socialaccount', 
    'user_account', 
    'widget_tweaks', 
 
] 
 
SITE_ID = 1 
 
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 = 'project_blog.urls' 
 
TEMPLATES = [ 
    { 
        'BACKEND': 'django.template.backends.django.DjangoTemplates', 
        'DIRS': [os.path.join(BASE_DIR, 'templates')], 
        '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 = 'project_blog.wsgi.application' 
 
 
# Database 
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases 
 
DATABASES = { 
    'default': { 
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 
 
 
# Password validation 
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators 
 
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', 
    }, 
] 
 
# Django-allauth 
AUTHENTICATION_BACKENDS = ( 
    # Needed to login by username in Django admin, regardless of `allauth` 
    'django.contrib.auth.backends.ModelBackend', 
 
    # `allauth` specific authentication methods, such as login by e-mail 
    'allauth.account.auth_backends.AuthenticationBackend', 
) 
 
# Internationalization 
# https://docs.djangoproject.com/en/1.10/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/1.10/howto/static-files/ 
 
STATIC_URL = '/static/' 
 
STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, 'static'), 
    #'/var/www/static/', 
] 
 
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn') 
 
MEDIA_URL = '/media/' 
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media_cdn') 
 
 
SUMMERNOTE_CONFIG = { 
    # Using SummernoteWidget - iframe mode 
    'iframe': True,  # or set False to use SummernoteInplaceWidget - no iframe mode 
 
    # Using Summernote Air-mode 
    'airMode': False, 
 
    # Use native HTML tags (`<b>`, `<i>`, ...) instead of style attributes 
    # (Firefox, Chrome only) 
    'styleWithTags': True, 
 
    # Set text direction : 'left to right' is default. 
    'direction': 'ltr', 
 
    # Change editor size 
    'width': '100%', 
    'height': '480', 
 
    # Authentication required 
    'attachment_require_authentication': False, 
} 
 
EMAIL_BACKEND='django.core.mail.backends.console.EmailBackend' 
 
# Custom allauth settings 
# Use email as the primary identifier 
ACCOUNT_AUTHENTICATION_METHOD = 'email'  
ACCOUNT_EMAIL_REQUIRED = True 
# Make email verification mandatory to avoid junk email accounts 
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'  
# Eliminate need to provide username, as it's a very old practice 
ACCOUNT_USERNAME_REQUIRED = False 
# Minimum password characters 
ACCOUNT_PASSWORD_MIN_LENGTH = 5 

请您参考如下方法:

根据 django docs ,你需要指定 OPTIONS dict 和内置的 django 密码验证器,像这样:

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