综合编程

Create a user-specific redirection after logging into django

微信扫一扫,分享到朋友圈

Create a user-specific redirection after logging into django
0 0

I have three types of people in my Django application Admin, Staff and Parent . how can I set up log-in for them as they will have different views after logging in using django registration? my code so far is this : in settings.py

LOGIN_REDIRECT_URL = '/app/login_redirect'
LOGIN_URL = '/app/accounts/login/'
LOGOUT_URL = '/app/accounts/logout/'

and in views.py:

def login_redirect(request):
    if request.user.is_authenticated() and not request.user.is_superuser:
        return HttpResponseRedirect(reverse("dashboard"))
    elif request.user.is_superuser:
        return HttpResponseRedirect(reverse("dashboard"))

I want to identify the which user is logged in ? I mean is it a staff user or a parent user or a admin? model.py

class Student(BaseModel):
    user = models.ForeignKey("auth.User",blank=True,null=True,related_name="user_%(class)s_objects")
    student_name = models.CharField(max_length=128)
    admission_number = models.CharField(max_length=128,null=True,blank=True)
    transportation_needs = models.CharField(max_length=128, choices=TRANSPORTATION_NEEDS,blank=True,null=True)
    extended_care_need = models.CharField(max_length=128, choices=EXTENDED_CARE_NEED,blank=True,null=True)
    nationality = CountryField (blank_label='------( select Your Country)-------',max_length=128,blank=True,null=True)
    date_of_birth = models.DateField()
    permanent_address = models.CharField(max_length=128,blank=True,null=True)
    address_for_communication = models.CharField(max_length=128,blank=True,null=True)
    gender = models.CharField(max_length=128, choices=GENDER_CHOICE)

Another way this can be achieved is by adding groups. Create group objects(with names as "partent", "student", "teacher") and add respective users to that group. By this way you dont have to override/change auth.User model.

def login_redirect(request):
     if request.user.is_authenticated():
         user_groups = request.user.groups.values_list('name', flat=True)
         if request.user.is_superuser:
             return HttpResponseRedirect(reverse("admin"))
         elif "parent" in user_groups:
             return HttpResponseRedirect(reverse("parent_dashboard"))
         elif "student" in user_groups:
             return HttpResponseRedirect(reverse("student_dashboard"))

Hope this helps.

阅读原文...

Hello, buddy!

Memcached - Why are data not stored?

上一篇

Redis 实战篇之搭建集群

下一篇

您也可能喜欢

评论已经被关闭。

插入图片
Create a user-specific redirection after logging into django

长按储存图像,分享给朋友