I cant figure out why Django User password is not accepted. I've tried creating multiple users and each time I try to log in Django tells that the username or password didnt match. It must be a password issue because each time I change the password manually in terminal python manage.py changepassword username
then the login is successful. I also checked that once the registration of the user is done then a user is added to the database and a hashed password is created as expected.
Any ideas what could be wrong here?
UPDATE
I also checked that the password works if I create a new user via django admin panel
models.py
class Company(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
views.py
def register(request):
registered = False
if request.method == 'POST':
user_form = UserCreateForm(data=request.POST)
profile_form = ProfileCreationForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
print(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
if 'logo' in request.FILES:
profile.logo = request.FILES['logo']
profile.save()
registered = True
else:
print(user_form.errors, profile_form.errors)
else:
user_form = UserCreateForm()
profile_form = ProfileCreationForm()
return render(request, 'companies/registration.html', {'user_form': user_form,
'profile_form': profile_form,
'registered': registered})
forms.py
class UserCreateForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = User
fields = ('username', 'email', 'password1', 'password2')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['username'].label = 'Lietotājvārds'