I'm using a custom user model that extends `AbstractBaseUser`. This is the 
user model:

class cUser(AbstractBaseUser):
    def save(self, *args, **kwargs):
        pass
    role_id = models.IntegerField()
    user_id = models.IntegerField()
    email = models.CharField(max_length=40)
    password = models.CharField(max_length=40)
    f_name = models.CharField(max_length=40)
    l_name = models.CharField(max_length=40)
    address_id = models.IntegerField()
    phone_num = models.IntegerField()
    loan_item_count = models.IntegerField()
    id = models.IntegerField(unique = True, primary_key = True)

    def __init__(self, dictionary, *args, **kwargs):
        self.role_id = int(dictionary['role_id'])
        self.user_id = dictionary['user_id']
        self.email = dictionary['email']
        self.password = dictionary['password']
        self.f_name = dictionary['f_name']
        self.l_name = dictionary['l_name']
        self.address_id = dictionary['address_id']
        self.phone_num = dictionary['phone_num']
        self.loan_item_count = dictionary['loan_item_count']
        self.id = self.user_id

    USERNAME_FIELD = 'user_id'
    class Meta:
        managed = False


I don't want the model to affect the DB in any way. I'm loading it by a 
simple raw SQL query from a gateway method.

This is how I'm handling login:


def login_request(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = userGateway(username,password)
            if user is not None:
                print("=========USER==========")
                print(user.email)
                user.backend = 'django.contrib.auth.backends.ModelBackend'
                login(request,user)
                print(request.user.is_authenticated)
                if user.role_id==1:
                    return render(request, 'biblioteca/admin/landing.html')
                    # return HttpResponseRedirect('/')
                else:
                    return render(request, 'biblioteca/landing.html')
            else:
                print("=========NOT USER==========")
    else:
        if(request.user is not None and not request.user.is_anonymous):
            return render(request, 'biblioteca/admin/landing.html')
        form = LoginForm()
        return render(request, 'biblioteca/login.html', {'form': form})


As you can see, I'm setting the back-end before login to authenticate 
without having to pass through a password - the password check is being 
done when the user object is created by comparing the password passed in 
with the password retrieved from the DB. The first check,

print(request.user.is_authenticated)

returns true, as expected. If I then return a render, the user object is 
still there. However, if I leave this page, or if I return a redirect 
instead, the user object becomes anonymous.


Any help with this would be greatly appreciated.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/fda76677-5313-4907-88b0-8a4a05aa97f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to