[Fixed]-Django login not working with custom user

1👍

The reason it doesn’t work is because I have named the view login, which is the same as the login() function builtin to django. Silly. Renamed it to ajax_login and everything works as expected.

👤lac

0👍

Your Django application is throwing an Error 500 because of the login(request, user) call. The reason for it failing is because you are passing in two arguments (request and user) even though the method definition only expects one argument to be passed in (which is request as specified in def login(request)). There seems to be something wrong with the logic I believe as you probably wouldn’t expect a recursive call at this point anyway.

Instead, I suggest you make use of the Django authentication system. It provides appropriate methods to authenticate users, for example:

from django.contrib.auth import authenticate
user = authenticate(username='john', password='secret')

You should have a closer look at the Django documentation where I have taken this code snippet from. There is a more sophisticated example on how to use the authentication system: Authenticating Users.

Leave a comment