1

I am using a custom user model with builtin and custom authentication backends. the default for username-password login and the custom one for uid based login The login is working fine but the 'user' (Other than superuser) is not available in the template.Nor is the last_login getting updated in database. Here is my code

backend.py

from support.models import CustomUser

class UsernameIdModelBackend(object):
    def authenticate(self, request, uid=None):
        try:
            user= CustomUser.objects.get(uid=uid)
            return user
        except CustomUser.DoesNotExist:
            return None

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

models.py

from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    uid = models.CharField(max_length=50)

Where did i go wrong..

EDIT :

I was using if user.is_authenticated before the 'user'. After removing the if condition i am seeing AnonymousUser.

p.ry
  • 409
  • 1
  • 8
  • 21
  • What do you mean by not available in the template? Is the login working fine? – oxalorg May 10 '19 at 06:08
  • @oxalorg i can login but if i go for something like user.username i wont get it. – p.ry May 10 '19 at 06:11
  • Have you confirmed that you have the Request context enabled for templates in your settings file? https://stackoverflow.com/questions/422140/how-to-access-the-user-profile-in-a-django-template – oxalorg May 10 '19 at 06:18
  • @oxalorg The django.contrib.auth.context_processors.auth inside templates-context-processors? yeah it's there. haven't changed it. – p.ry May 10 '19 at 07:19

0 Answers0