一个django可重用的应用程序,它提供验证器和一个检查密码强度的表单字段
django-passwords的Python项目详细描述
django密码
django passwords是一个可重用的应用程序,它提供一个表单字段和 检查密码强度的验证器。
安装
通过键入:
pip install django-passwords
或者通过键入:
easy_install django-passwords
或者手动下载tarball并键入:
python setup.py install
兼容性
django密码与django 1.3到1.9rc1兼容。Python2.7 和3.4都支持。
设置
django密码添加6个可选设置
- 可选:
指定密码的最小长度:
PASSWORD_MIN_LENGTH = 6 # Defaults to 6
指定密码的最大长度:
PASSWORD_MAX_LENGTH = 120 # Defaults to None
指定字典的位置(每行一个单词的文件):
PASSWORD_DICTIONARY = "/usr/share/dict/words" # Defaults to None
指定将模糊匹配视为匹配的程度:
PASSWORD_MATCH_THRESHOLD = 0.9 # Defaults to 0.9, should be 0.0 - 1.0 where 1.0 means exactly the same.
指定要尝试将密码与之匹配的常见序列列表:
PASSWORD_COMMON_SEQUENCES = [] # Should be a list of strings, see passwords/validators.py for default
指定密码必须包含的不同集合中的字符数:
PASSWORD_COMPLEXITY = { # You can omit any or all of these for no limit for that particular set "UPPER": 1, # Uppercase "LOWER": 1, # Lowercase "LETTERS": 1, # Either uppercase or lowercase letters "DIGITS": 1, # Digits "SPECIAL": 1, # Not alphanumeric, space or punctuation character "WORDS": 1 # Words (alphanumeric sequences separated by a whitespace or punctuation character) }
使用量
要使用formfield,只需导入并使用它:
fromdjangoimportformsfrompasswords.fieldsimportPasswordFieldclassExampleForm(forms.Form):password=PasswordField(label="Password")
您可以在自己的字段上使用验证器:
fromdjangoimportformsfrompasswords.validatorsimportdictionary_wordsfield=forms.CharField(validators=[dictionary_words])
您还可以创建自定义验证器实例来指定自己的 特定于字段的配置,而不是使用全局 配置:
fromdjangoimportformsfrompasswords.validatorsimport(DictionaryValidator,LengthValidator,ComplexityValidator)field=forms.CharField(validators=[DictionaryValidator(words=['banned_word'],threshold=0.9),LengthValidator(min_length=8),ComplexityValidator(complexities=dict(UPPER=1,LOWER=1,DIGITS=1)),])