1👍
Your code snippets are all correct , but the way of saving the user’s password is incorrect in the from via save method , the way you are saving the password , it saves the raw text form of the password to the database , if you want to check , just open your database and check the password fields , they are stored in raw text format ( exapmle : testing123) where as the django saves , retrieves , password using password hashing alogrithm of sha256 until and unless you have not specified it and its hashes to pbkdf2_sha256… this format .
dont save user like this :
user = User(username = username , password = password , email = email)
user.save()
save like this
user = User(username = username , email = email)
user.set_password(password)
user.save()
Update your code snippet :
from django import forms
from django.contrib.auth import get_user_model, authenticate, login
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = get_user_model()
fields = ['username', 'email', 'first_name']
def save(self):
self.clean()
user = self.Meta.model(
username = self.cleaned_data['username'],
email = self.cleaned_data['email'],
)
user.set_password(self.cleaned_data['password2'])
user.save()
return user
This will do the required work.
0👍
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = get_user_model()
fields = ['username', 'email', 'first_name']
def save(self):
self.clean()
user = self.Meta.model(
username = self.cleaned_data['username'],
email = self.cleaned_data['email'],
)
# Set password with method is solution
user.set_password(self.cleaned_data['password2'])
user.save()
return user
- [Answered ]-Django: provide month and year number as parameter in a template for use in template tag
- [Answered ]-Error reversing django migrations with sql_safe_update mode set in MariaDB
0👍
The reason this does not work is because passwords are hashed, and your UserRegisterForm
does not hash the password properly. There is however no need to override the .save(…)
method. Django’s UserCreationForm
[Django-doc] already takes care of this properly, since it is a ModelForm
[Django-doc], so:
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = get_user_model()
fields = ['username', 'email', 'first_name']
# no override of save
- [Answered ]-Django detecting not needed changes
- [Answered ]-Python query to game server
- [Answered ]-How to remove label?