How to restrict my students to don't access teacher area in django? [closed]

泪湿孤枕 提交于 2019-12-18 09:52:17

问题


I'm creating a website where there is two types of users, Students and Teachers. I had created one register and login page for authentication. Now by default all the users who sign up will be the students and can access the videos and also accessing teachers area (I did the coding till here).

If someone want to be the teacher there, then they have to submit their documents. After the verification, they can access teachers page. The problem is that I don't know how to give authorization to these user manually from admin panel so that they can access to specific (teachers) page?

I had google it a lot but don't understand how to do this thing. My website is ready but I don't know how to restrict all the users from teachers page and after their documents verification, how to give permission to access the teachers page.


回答1:


You can create your own view decorator that checks that a user is a member of a group

from django.contrib.auth.decorators import user_passes_test

def is_teacher(user):
    return user.groups.filter(name='Teacher').exists()

@user_passes_test(is_teacher)
def my_view(request)
    ...

Docs for user_passes_test

Then all you have to do is create a Group with name "Teacher" and add teachers to that group




回答2:


Try using the is_staff field from built-in User model. For teachers, set it to true and for children set it to false. On the basis of it decide the rights given to each. Hope it helps.



来源:https://stackoverflow.com/questions/57338608/how-to-restrict-my-students-to-dont-access-teacher-area-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!