问题
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