I've made a register and login function which saves a user data to the database using the django User object. But when i register a user, the password linked to the user doesn't get hashed properly. This means that i have this error in the django admin interface: "Invalid password format or unknown hashing algorithm.". I've made sure to use the set_password.
models.py
from django.db import models
from django.contrib.auth.models import User
class User_Information(models.Model):
# Links UserProfile to a User model instance
user = models.OneToOneField(User)
# Override the __unicode__() method to return username
def __unicode__(self):
return self.username
forms.py
from django import forms
from django.contrib.auth.models import User
from authentication.models import User_Information
class User_Form(forms.ModelForm):
# Using the PasswordInput widget to hide the entered content of the password field
password = forms.CharField(widget=forms.PasswordInput())
# Define the nested class. The default fields can be edited here, if you wish to exclude something.
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password')
views.py
def register(request):
context = RequestContext(request)
# Checks if registration was successful. Changes to true if this is the case
# Processing form data.
if request.method == 'POST':
user_form = User_Form(data=request.POST)
# If the form is valid.
if user_form.is_valid():
# Saves the user's data to the database.
user = user_form.save()
# Hash the password and updates the user object.
user.set_password(user.password)
user.save
# Tell the template that registration was successful
messages.success(request, 'You registered successfully')
else:
print user_form.errors
else:
user_form = User_Form()
return render_to_response(
'authentication/register.html',
{'user_form': user_form},
context)
Thanks in advance.
It is possible to extend predefined form django.contrib.auth.forms.UserCreationForm to your needs.