我正在尝试使用django-registration 1.0但是当我按下注册按钮时我卡住了。这就是我得到的。
TypeError at /accounts/register/
Unicode-objects must be encoded before hashing
Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/register/
Django Version: 1.5.1
Exception Type: TypeError
Exception Value:
Unicode-objects must be encoded before hashing
Exception Location: C:\Python33\lib\site-packages\registration\models.py in
TRACEBACK COPY-PASTE MODE
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/register/
Django Version: 1.5.1
Python Version: 3.3.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.contenttypes',
'django.contrib.admin',
'MyDjangoApp',
'Books',
'registration',
'django.core.mail',
'user_profile')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')
Traceback:
File "C:\Python33\lib\site-packages\django\core\handlers\base.py" in get_response
115. response = callback(request, *callback_args,
**callback_kwargs)
File "C:\Python33\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "C:\Python33\lib\site-packages\registration\views.py" in dispatch
79. return super(RegistrationView, self).dispatch(request, *args, **kwargs)
File "C:\Python33\lib\site-packages\django\views\generic\base.py" in dispatch
86. return handler(request, *args, **kwargs)
File "C:\Python33\lib\site-packages\registration\views.py" in post
35. return self.form_valid(request, form)
File "C:\Python33\lib\site-packages\registration\views.py" in form_valid
82. new_user = self.register(request, **form.cleaned_data)
File "C:\Python33\lib\site-packages\registration\backends\default\views.py" in register
80. password, site)
File "C:\Python33\lib\site-packages\django\db\transaction.py" in inner
223. return func(*args, **kwargs)
File "C:\Python33\lib\site-packages\registration\models.py" in create_inactive_user
88. registration_profile = self.create_profile(new_user)
File "C:\Python33\lib\site-packages\registration\models.py" in create_profile
106. salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
Exception Type: TypeError at /accounts/register/
Exception Value: Unicode-objects must be encoded before hashing
任何可能出错的建议?除了安装应用程序以及对网址和设置文件进行一些细微更改之外,我还没有做任何其他事情。
答案 0 :(得分:1)
问题是你使用的是Python 3;并且django-registration
尚未与之兼容。您需要使用Python 2.7.x
答案 1 :(得分:1)
我的django-registration + Python 3.3遇到了同样的问题,并通过更改django-registration的 models.py 的 create_profile 函数中的几行来解决问题。
问题是'utf-8'编码的字符串与hashlib.sha1哈希函数兼容。
因此我重写了以下代码块
salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
username = user.username
if isinstance(username, unicode):
username = username.encode('utf-8')
activation_key = hashlib.sha1(salt+username).hexdigest()
这个
salt = hashlib.sha1(str(random.random()).encode('utf-8')).hexdigest()[:5]
salted_username = salt + user.username
activation_key = hashlib.sha1(salted_username.encode('utf-8')).hexdigest()
在将字符串对象作为参数传递之前对其进行编码似乎解决了我的问题。
答案 2 :(得分:0)
我认为密码必须在散列之前用utf-8编码。似乎django-registration 1.0 ha有很多问题。
答案 3 :(得分:0)
正如其他人所说,问题是django-registration不支持Python 3。
2013年9月的程序包stopped maintaining it的作者。似乎django-allauth目前是最适合Python 2和3的替换(由pydanny推荐)。