django.contrib.auth

If i use a db other than the one the django’s default db will the libraries or functions like django.contrib.auth or login(request, user) will work?
As I am unable to keep a usr logged in after authentication? I read a post in stackoverflow where it says i need to detach user_logged_in signal. Can anyone guide me through this or help me with some link where it explains how to do this.

Thanks a lot everyone.

What db are you talking about using?

I typically recommend against searching through SO for Django-related issues. The biggest problem is that old answers hang around forever, and so you need to be extremely aware of how old an answer is, and whether or not it’s at all relevant to a current version of Django.

What are you doing that isn’t working?

If you could be more specific as to what it is you’re trying to do, we might be able to provide more targeted advice.

Thanks Ken for always being around.
I am using MongoDB.
i am creating a page where I post the disease in a human eye (this is related to ML) but to access this page user has to login. So, when user login its get authenticated using the custom authentication backened which I created. Why I created because as per my understanding dhango.contrib.auth is used when we use the default django’s default db.
So I asked whether my understanding is correct or not.

Now the issue here is that I cannot keep the user logged in.
Code below:

def cus_login(request):
    if request.method =='POST':
        print("I am here1")
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')

            user = LoginAuthBackened().authenticate(request=request, username=username, password=password)
            if user:
               #  login(request, user)
                # if 'next' in request.POST:
                #     print('here', request.POST.get('next'))
                #     return redirect(request.POST.get('next'))

                messages.success(request, f'Login Successful.')
                print('Inside Login View',request.user)
                print('Inside Login View', request.user.is_authenticated)
                return redirect('app-workspace')
            else:
                messages.error(request, f'Password Incorrect !')
                return redirect('app-login')
        else:
            print(form.errors)
    else:
        form = LoginForm()
    return render(request, 'users/login.html', {'form': form})

Backend:

from .models import StaffDataModel

class LoginAuthBackened():

def authenticate(self, request, username=None, password=None):
    try:
        print(username, password)
        user = StaffDataModel.objects.get(username=username)
        if user.password == password:
            return user
        else:
            return False

    except Exception as e:
      print(e)
    return None

def get_user(self, user_id):
    try:
        return StaffDataModel.objects.get(pk=user_id)
    except:
        return None

There is one thing I notice up front, this isn’t how you use your custom authentication

When you’re implementing a custom authentication backend, it does not change your application. (See Authenticating users).

The system authenticate method calls all appropriate authentication backends.

Beyond that, everything else looks ok at the moment. What makes you think that the user isn’t logged in when you get to the ‘app-workspace’ page?

1 Like

This piece of code returns “False” after authentication as it doesnot have any information about the user.
image

and this code:

throws this error:

I mentioned above that you’re not supposed to call your authenticate method directly.

You must call the login method. If the login method is throwing an error, then that’s a different issue that needs to be addressed.

So I should pass the authentication function inside the login function?

Assuming everything else is correct, your login function should look more like this:

def cus_login(request):
    if request.method =='POST':
        print("I am here1")
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')

            user = authenticate(request, username=username, password=password)
            if user:
                login(request, user)
                messages.success(request, f'Login Successful.')
                print('Inside Login View',request.user)
                print('Inside Login View', request.user.is_authenticated)
                return redirect('app-workspace')
            else:
                messages.error(request, f'Password Incorrect !')
                return redirect('app-login')
        else:
            print(form.errors)
    else:
        form = LoginForm()
    return render(request, 'users/login.html', {'form': form})

Then, you made a reference to an error being thrown in the login method - for that we’ll probably need more information, starting with your user model.

Thanks for the code Ken. I found the error after reading the link you provided. Actually why I thought the error is related to authentication because I was not using the standard authentication method which in turn is creating a problem for the login function.

But actually the problem was in model. How is that related is, the login function takes two args request and user. This user should have last_login field, if I had used the standard user model then I no need to explicitly create this last_login. So, when I created that field everything worked well.
And also implemented “is_authenticated” method too in my UserModel class.

Thanks a lot !!! :slight_smile: