0

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'
d1spstack
  • 930
  • 1
  • 5
  • 16
  • I don't see any problem in the code you have provided. The problem must be somewhere else. – David Louda Aug 03 '21 at 11:32
  • may I suggest to check the sanity list provided in this thread [link](https://stackoverflow.com/questions/11894765/unable-log-in-to-the-django-admin-page-with-a-valid-username-and-password) , at list you can know "what's not the problem". – Carlo Aug 03 '21 at 12:17

1 Answers1

0

I deleted this line from views.py and it worked

user.set_password(user.password)

I cant figure out why it works now since I don't recall changing this line of code and I use it in my other projects since its also used in the tutorials from which I'm learning Django

UPDATE

Cause found. The code user.set_password(user.password) actually set a new password based on the hash value of the initial password that I entered during user creation. After filling the form the user.password didn't represent the raw password but rather the hashed version of it. So my code created two password

d1spstack
  • 930
  • 1
  • 5
  • 16