在Django中保持检查帐户类型的最佳方法

时间:2019-03-28 05:36:29

标签: django

我有一个有关学校系统的项目,该项目有3种类型的用户 StaffStudentLecturer

类型为Staff的用户的url配置将具有一个类似.com/staff/ something/something的url。例如:.com/staff/account-management 同样,.com/student/something/something代表Student个用户,{{11}}代表.com/lecturer/

如果学生访问Lecturers用户的网页或类似的网址,例如Staff.com/staff/account-management,我想显示一个网页,显示您没有权限访问此页面

最后,一种类型的用户无法访问另一种用户类型的网址。

此外,每次我加载.com/staff/resources-management网页时,我都需要名字和全名以及注册的课程。 对于student网页,我需要名字和全名以及讲师教的课。 对于lecturer网页,我只需要名字和全名。 这是由其他模板扩展的base.html用途 此base.html包含需要这些信息的导航栏

这是我的方法:

在学生staff中,有一个名为views.pycheck_user的视图

student_homepage

check_user(request) user = request.user std = Student.objects.filter(user=user).first() if std is not None : get all of the classes he is currently studying and put it in class_display context = {'role' : 'STUDENT', 'f_name' : user.first_name, 'l_name' : user.last_name, 'class_display' = class_display} return context else: check if the user is an Employee object if it is an Employee object, check if it is a staff or a lecturer if it is a staff, context = {'role' : 'STAFF', 'f_name' : user.first_name, 'l_name' : user.last_name, msg='You are not authorized to see this page'} if it is a lecturer, get all the classes he is currently teaching and put it in class_display context = {'role' = 'LECTURER', 'f_name' : user.first_name, 'l_name' : user.last_name, msg='You are not authorized to see this page','class_display = class_display} if it is not student or employee context = {'role' = 'none', msg='Unidentified account type','class_display = class_display} 扩展了base.html,errorpage.html具有一个按钮,供errorpage.html在读取错误消息后返回其lecturer/administrative

homepage

这行得通,但似乎是一种不好的方法,因为对于呈现学生页面的每个学生视图,我必须检查用户的角色。有没有一种方法只能编写一次,但它适用于以.com / student开头的所有URL?我有一个URL .com / student的视图,该URL最初将重定向到学生主页,但我认为只有在我转到.com / student时才能调用它。

0 个答案:

没有答案
相关问题