Zamphatta
Zamphatta

Reputation: 4714

authenticate() Always Returns None

I've successfully created a user with create_user(), and the user can log into the admin section. BUT, making code in my views.py so the user can log in anywhere else, isn't working. Here's the code I'm using, and even when the username & password are definitely correct, authenticate() still returns None.

def dologin(request):
    usrnym = request.POST['usrnym']
    psswrd = request.POST['usrpass']
    usr = authenticate(user=usrnym,password=psswrd)
    if usr is not None:
        if usr.is_active:
            login(request, usr)
            # redirect to success page
        else:
            pass
            # redirect to a 'disabled account' error message
    else:
        # return an 'invalid login' error message
        errmsg = "Invalid login. Password and username did not match."
        template = loader.get_template('fsr/loginform.html')
        context = RequestContext(request, {
            'title': "Login Page",
            'error': errmsg,
            'usr': usrnym,
            'pwd': psswrd,
            'usra': usr,
        })
        return HttpResponse(template.render(context))

Upvotes: 0

Views: 452

Answers (2)

Vishnu667
Vishnu667

Reputation: 768

it should be username not user its a syntax error

usr = authenticate(username = usrnym, password = psswrd)

Upvotes: 2

eandersson
eandersson

Reputation: 26342

I might be wrong, but isn't it supposed to be?

usr = authenticate(username=usrnym,password=psswrd)

Upvotes: 2

Related Questions