I have multiple user models in my Django app, one for OLAROOM
and the other one for REALTOR
. As you can see, everything is working successfully, but when I try to authenticate them and redirect them based on their role, it does not work. The authentication is working fine, but instead of redirecting the Olaroom
user to the Rooms page, it is redirecting me to the Realtor_Dashboard
, and the user role isn't a Realtor. Using the form below, the user is created based on what is defined in the save function. However, when I try to authenticate them and redirect the user to the page based on their role, it doesn't work, and I'm using the same method, something like this: user.Role.REALTOR
and user.Role.OLAROOM
. How can I solve this problem please?
def login(request):
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.Role.REALTOR:
auth_login(request, user)
return redirect('Realtor_Dashboard')
elif user.Role.OLAROOM:
auth_login(request, user)
return redirect('Rooms')
else:
messages.error(request, 'Invalid Creadentials')
else:
form = AuthenticationForm()
return render(request, 'Login/login.html', {'form':form})
User Models
class User(AbstractUser):
class Role(models.TextChoices):
ADMIN = "ADMIN", 'Admin'
REALTOR = "REALTOR", 'Realtor'
OLAROOM = "OLAROOM", 'Olaroom'
base_role = Role.ADMIN
role = models.CharField(max_length=225, choices=Role.choices)
username = models.CharField(max_length=15, unique=True)
first_name = models.CharField(max_length=225)
last_name = models.CharField(max_length=225)
email = models.EmailField(unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
def save(self, *args, **kwargs):
if not self.pk:
self.role = self.base_role
return super().save(*args, **kwargs)
def __str__(self):
return str(self.username)
class OlaroomsManger(BaseUserManager):
def get_queryset(self, *args, **kwargs):
results = super().get_queryset(*args, **kwargs)
return results.filter(role=User.Role.OLAROOM)
class Olaroom(User):
base_role = User.Role.OLAROOM
company_name = models.CharField(max_length=225)
address = models.CharField(max_length=225)
phone_number = models.CharField(max_length=15)
country = CountryField()
olaroom = OlaroomsManger()
def welcome(self):
return "Only for Olaroom"
class RealtorManger(BaseUserManager):
def get_queryset(self, *args, **kwargs):
results = super().get_queryset(*args, **kwargs)
return results.filter(role=User.Role.REALTOR)
class Realtor(User):
base_role = User.Role.REALTOR
address = models.CharField(max_length=225)
phone_number = models.CharField(max_length=15)
country = CountryField()
state = models.CharField(max_length=225)
realtor = RealtorManger()
def welcome(self):
return "Only for Realtor"
forms:
class OlaroomSignUpForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Olaroom
fields = ['username', 'email', 'company_name', 'address', 'phone_number', 'country']
def save(self, commit=True):
user = super().save(commit=False)
user.Role.OLAROOM
if commit:
user.save()
return user
class RealtorSignUpForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Realtor
fields = ['username', 'email', 'first_name', 'last_name', 'address', 'phone_number', 'country', 'state']
def save(self, commit=True):
user = super().save(commit=False)
user.Role.REALTOR
if commit:
user.save()
return user
the issue i'm facing is related to how i'm checking the user's role and trying to redirect them after authentication. In my code, i'm checking user.Role.REALTOR
and user.Role.OLAROOM
, but this is not the correct way to check the user's role based on my models.
Instead, I should check the role attribute of the user instance to determine their role and redirect accordingly.
def login(request):
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.role == User.Role.REALTOR:
auth_login(request, user)
return redirect('Realtor_Dashboard')
elif user.role == User.Role.OLAROOM:
auth_login(request, user)
return redirect('Rooms')
else:
messages.error(request, 'Invalid Credentials')
else:
form = AuthenticationForm()
return render(request, 'Login/login.html', {'form': form})
In this updated code, Instead of using user.Role.REALTOR
or user.Role.OLAROOM
, I'm using user.role == User.Role.REALTOR
and user.role == User.Role.OLAROOM
which means: If the user's role is REALTOR
, it logs in the user using auth_login and redirects to a Realtor_Dashboard
page. If the user role is OLAROOM
, it logs in the user and redirects to a Rooms
page.