ImportError:无法导入名称get_hexdigest
即时通讯运行到这个问题,因为我试图在我的osX10.7,python2.7 django1.4系统上运行一段django代码。我如何获得get_hexdigest?我从哪里下载它?ImportError:无法导入名称get_hexdigest
Kinnovates-MacBook-Pro:platformsite Kinnovate$ sudo python manage.py runserver
Running in development mode.
Running in development mode.
Running in development mode.
Running in development mode.
Validating models...
HACKUING USER MODEL
Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x1016bc050>>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/validation.py", line 30, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 158, in get_app_errors
self._populate()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 67, in _populate
self.load_app(app_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/loading.py", line 88, in load_app
models = import_module('.models', app_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_sha2-0.4-py2.7.egg/django_sha2/models.py", line 2, in <module>
from django_sha2 import auth
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_sha2-0.4-py2.7.egg/django_sha2/auth.py", line 96, in <module>
monkeypatch()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_sha2-0.4-py2.7.egg/django_sha2/auth.py", line 42, in monkeypatch
from django_sha2 import bcrypt_auth
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_sha2-0.4-py2.7.egg/django_sha2/bcrypt_auth.py", line 10, in <module>
from django.contrib.auth.models import get_hexdigest
ImportError: cannot import name get_hexdigest
您正在使用Django(1.4)的dev的版本,并且在对应的模块没有get_hexdigest
方法。
解决方案:
- 使用1.3版本(这是最后稳定在目前)
- 实现
get_hexdigest
自己(可以从here被copypasted) - 使用另一种工具(即不具有兼容性问题)来解决你的任务
我设法做到了第二种方式,它正在工作,但我不确定这是否是一种正确的方式来做到这一点,以及是否会产生其他问题。你的回答让我感觉更安全。谢谢! – henghonglee 2012-02-18 16:06:54
你是否正在使用它来加密密码进行比较。原来的Django 1.5(1.4另?)现在提供一个更好的效用函数:
https://docs.djangoproject.com/en/dev/topics/auth/passwords/#auth-password-storage
具体做法是:
check_password(密码,编码) 如果你想手动身份验证用户通过将明文密码与数据库中的哈希密码进行比较,使用便利功能check_password()。它有两个参数:要检查的纯文本密码以及数据库中要检查的用户密码字段的完整值,如果匹配则返回True,否则返回False。
make_password(密码[,盐,hashers]) 创建在由本申请中使用的格式的哈希密码。它需要一个强制参数:以纯文本形式输入密码。或者,如果您不想使用默认值(PASSWORD_HASHERS设置的第一项),则可以提供使用的salt和散列算法。目前支持的算法是:'pbkdf2_sha256','pbkdf2_sha1','bcrypt_sha256'(参见使用bcrypt和Django),'bcrypt','sha1','md5','unsalted_md5'(仅用于向后兼容)和'crypt'if你已经安装了crypt库。如果密码参数是None,则返回一个不可用的密码(一个将不会被check_password()接受的密码)。
is_password_usable(encoded_password) 检查,如果给定的字符串是具有被证实对check_password机会()散列密码。
遗留代码
def check_master_password(raw_password):
from django.conf import settings
from django.contrib.auth.models import get_hexdigest
enc_password = getattr(settings, 'MASTER_PASSWORD', None)
if enc_password:
algo, salt, hsh = enc_password.split('$')
return hsh == get_hexdigest(algo, salt, raw_password)
新的1.5代码
def check_master_password(raw_password):
from django.conf import settings
from django.contrib.auth.hashers import check_password
return check_password(raw_password, getattr(settings, 'MASTER_PASSWORD', None))
实现自己的方法(使用hashlib
代替hashcompat
):
import hashlib
from django.utils.encoding import smart_str
def get_hexdigest(algorithm, salt, raw_password):
"""
Returns a string of the hexdigest of the given plaintext password and salt
using the given algorithm ('md5', 'sha1' or 'crypt').
"""
raw_password, salt = smart_str(raw_password), smart_str(salt)
if algorithm == 'crypt':
try:
import crypt
except ImportError:
raise ValueError('"crypt" password algorithm not supported in this environment')
return crypt.crypt(raw_password, salt)
if algorithm == 'md5':
return hashlib.md5(salt + raw_password).hexdigest()
elif algorithm == 'sha1':
return hashlib.sha1(salt + raw_password).hexdigest()
raise ValueError("Got unknown password algorithm type in password.")
也许你应该张贴在创建代码这个错误。特别是用户模型的黑客攻击 – 2012-02-17 07:36:21
Django安装程序出错。该功能应该在那里。尝试打开文件本身,在那里? – DrTyrsa 2012-02-17 07:54:38
我在哪里可以找到这个文件? – henghonglee 2012-02-17 08:09:16