Forums

Django admin password not working

I am new to Django and I was creating a simple user registration page. I am able to create new users from my register.html page. I can see the users are created in the django's built-in administration page. However when I try to login into my website with the newly created user, I am redirected to error page.

def newuser(request):
if request.method == "POST":
    email = request.POST.get('email','')
    password = request.POST.get('password','')
    user, created = User.objects.get_or_create(username=email, email=email)
    if created:
        user.set_password(password)
        user.save()
        return HttpResponseRedirect('/master/messages')
else:
        return HttpResponseRedirect('/master/invalid')

It seems that the password is not getting saved in hashed format. When I reset the password from admin page, I can login with the new password. Can anyone suggest how can I fix this?

That certainly looks like it should work fine. Perhaps a question for Stack Overflow?

I have asked this on stackoverflow as well.

As the user is getting created, I am sure that the issue is with the way I am setting the password. Any sample code or link of some documentation with example would be of great help. Any links?

The thing is, I'm pretty sure that your code is correct! This code from the Django source itself uses the same method (see the save method in UserCreationForm).

One question -- which login page do you mean? Your own one, or the one for the Django admin?

My own one. This is the code that i have used for authentication -

 def auth_view(request):
    username = request.POST.get('email','')
    password = request.POST.get('password','')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        return HttpResponseRedirect('/accounts/success')
    else:
        return HttpResponseRedirect('/accounts/failure')

So I am getting redirected to the failure page. But if I reset the password from django administration page and use the new password, then I am redirected to the success page.

That looks fine too. I really don't know why that might not be working.