Reputation: 116
I want to display the home page of my site based on the user type (admin, guest, student, etc.). It would be nice to display a link to the login page for guest users, but this link should be hidden for already authenticated users. For admins there should be a link to django-admin. Besides that there also some differences in the home page for other user roles.
What is a good way to implement this? I have several options for that:
home_guest.html
, home_admin.html
, ...{% if user.is_authenticated %}
Upvotes: 1
Views: 1015
Reputation: 21
You can start with the documentation, using:
from django.contrib.auth.decorators import user_passes_test
def email_check(user):
return user.email.endswith('@example.com')
@user_passes_test(email_check)
def my_view(request):
...
which roughly means that if the user passes the test - provided on the email endswith
here - then they will be able to see a view. In my case, I deviated a bit from this and created a
class User(AbstractBaseUser):
status: 1,2 per se, in my model:
student=1
teacher=2
STATUS_CHOICES = ((student, "student"),(teacher, "teacher"),)
status = models.IntegerField(choices = STATUS_CHOICES)
This lets you create an is_status
property:
@property
def is_status (self):
return self.status
And then in your login view, you can play along with the properties. If it is 1, display this view, otherwise, display this other one.
Upvotes: 2