user3007270
user3007270

Reputation: 406

Django-registration 1.0 Registration not working

I am trying to work the django-registration 1.0 but I am stuck when I hit the register button. This is what I get.

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

Any suggestion of what can be wrong? I have not done anything other than install the app and some minor changes to the url, and settings files.

Upvotes: 1

Views: 641

Answers (4)

metakermit
metakermit

Reputation: 22311

As others said, the problem is that django-registration doesn't support Python 3.

The author of the package stopped maintaining it in September 2013. Seems that django-allauth is currently the best replacement that works in both Python 2 and 3 (recommended by pydanny).

Upvotes: 0

yildirim
yildirim

Reputation: 314

I had the same problem with my django-registration + Python 3.3 and solved the issue by changing few lines in create_profile function of django-registration's models.py.

The problem is only 'utf-8' encoded strings are compatible with the hashlib.sha1 hashing function.

Therefore I have rewritten the following code block

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()

as this

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()

Encoding the string object before passing it as an argument seems to solve the problem in my case.

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174624

The problem is you are using Python 3; and django-registration is not yet compatible with it. You need to use Python 2.7.x

Upvotes: 1

Midimo
Midimo

Reputation: 107

I think the password must be encode in utf-8 before hash it. seems like django-registration 1.0 ha a lot of problems.

Upvotes: 0

Related Questions