Django Unicode解码错误
Django UnicodeDecodeError
我尝试实现 this 哈希器以便能够验证从 drupal 7 导入的用户。
所以我把散列器放在 settings.py 中,如下所示:
PASSWORD_HASHERS = (
'myproj.drupal_hasher.DrupalPasswordHasher', #drupal hasher
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
'django.contrib.auth.hashers.SHA1PasswordHasher',
'django.contrib.auth.hashers.MD5PasswordHasher',
'django.contrib.auth.hashers.CryptPasswordHasher',
)
但是它不会对从 drupal 导入的用户进行身份验证,对于在 Django 中创建的用户,我得到:
'ascii' codec can't decode byte 0xcf in position 0: ordinal not in range(128)
引用如下:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/auth/
Django Version: 1.7.3
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'article',
'photo',
'debug_toolbar',
'django_markdown',
'haystack')
Installed Middleware:
(u'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mario/myproj/myauth/views.py" in auth_view
16. user = auth.authenticate(username=username, password=password)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in authenticate
60. user = backend.authenticate(**credentials)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/backends.py" in authenticate
17. if user.check_password(password):
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in check_password
237. return check_password(raw_password, self.password, setter)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/hashers.py" in check_password
63. setter(password)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in setter
235. self.set_password(raw_password)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in set_password
227. self.password = make_password(raw_password)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/hashers.py" in make_password
85. return hasher.encode(password, salt)
File "/home/mario/myproj/myproj/drupal_hasher.py" in encode
81. encoded_hash = self._apply_hash(password, self._digests[digest], settings)
File "/home/mario/myproj/myproj/drupal_hasher.py" in _apply_hash
68. password_hash = digest(password_hash + password).digest()
Exception Type: UnicodeDecodeError at /auth/
Exception Value: 'ascii' codec can't decode byte 0xcf in position 0: ordinal not in range(128)
我尝试将 .decode('utf-8') 解码为 encoded_hash,但得到了相同的结果。
所有源和目标数据库表都转换为 utf8_unicode_ci。我添加了
#-*- coding: utf-8 -*-
在所有相关页面的顶部。
几天来我一直在解决这个问题,非常感谢您的帮助。
首先,将'drupal$'附加到导入的密码:
(因为 drupal 是默认的哈希器,用户 1 已经将 drupal$ 哈希到他的密码)
UPDATE djangodb.auth_user SET password=CONCAT('drupal$',password) where id<>1;
然后才发现要加
password = password.encode('ascii','ignore')
在 DrupalPasswordHasher class 中 'encode' 和 'verify' 方法的开头,因为我的应用程序中的密码被编码为 utf,但散列器需要 ascii。
这些简单的要点起到了作用。
我尝试实现 this 哈希器以便能够验证从 drupal 7 导入的用户。
所以我把散列器放在 settings.py 中,如下所示:
PASSWORD_HASHERS = (
'myproj.drupal_hasher.DrupalPasswordHasher', #drupal hasher
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
'django.contrib.auth.hashers.SHA1PasswordHasher',
'django.contrib.auth.hashers.MD5PasswordHasher',
'django.contrib.auth.hashers.CryptPasswordHasher',
)
但是它不会对从 drupal 导入的用户进行身份验证,对于在 Django 中创建的用户,我得到:
'ascii' codec can't decode byte 0xcf in position 0: ordinal not in range(128)
引用如下:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/auth/
Django Version: 1.7.3
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'article',
'photo',
'debug_toolbar',
'django_markdown',
'haystack')
Installed Middleware:
(u'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mario/myproj/myauth/views.py" in auth_view
16. user = auth.authenticate(username=username, password=password)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in authenticate
60. user = backend.authenticate(**credentials)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/backends.py" in authenticate
17. if user.check_password(password):
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in check_password
237. return check_password(raw_password, self.password, setter)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/hashers.py" in check_password
63. setter(password)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in setter
235. self.set_password(raw_password)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in set_password
227. self.password = make_password(raw_password)
File "/home/mario/.myprojenv/local/lib/python2.7/site-packages/django/contrib/auth/hashers.py" in make_password
85. return hasher.encode(password, salt)
File "/home/mario/myproj/myproj/drupal_hasher.py" in encode
81. encoded_hash = self._apply_hash(password, self._digests[digest], settings)
File "/home/mario/myproj/myproj/drupal_hasher.py" in _apply_hash
68. password_hash = digest(password_hash + password).digest()
Exception Type: UnicodeDecodeError at /auth/
Exception Value: 'ascii' codec can't decode byte 0xcf in position 0: ordinal not in range(128)
我尝试将 .decode('utf-8') 解码为 encoded_hash,但得到了相同的结果。
所有源和目标数据库表都转换为 utf8_unicode_ci。我添加了
#-*- coding: utf-8 -*-
在所有相关页面的顶部。
几天来我一直在解决这个问题,非常感谢您的帮助。
首先,将'drupal$'附加到导入的密码:
(因为 drupal 是默认的哈希器,用户 1 已经将 drupal$ 哈希到他的密码)
UPDATE djangodb.auth_user SET password=CONCAT('drupal$',password) where id<>1;
然后才发现要加
password = password.encode('ascii','ignore')
在 DrupalPasswordHasher class 中 'encode' 和 'verify' 方法的开头,因为我的应用程序中的密码被编码为 utf,但散列器需要 ascii。
这些简单的要点起到了作用。