How to restrict my students to don't access te

2019-09-20 10:07发布

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.

2条回答
老娘就宠你
2楼-- · 2019-09-20 10:35

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.

查看更多
冷血范
3楼-- · 2019-09-20 10:52

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

查看更多
登录 后发表回答