Hello,
I would like to hear your opinion on how to allow different users to have access to different sections/pages of the site.
Of course, Django offers the built-in permissions system, which I am using extensively. I am facing the following challenge:
Some pages are not directly associated with a model, so it does not make much sense to use any of the provided permissions (e.g. add/change/delete
). Neither a custom permission, since again, it needs to be attached to a model. In an example application, both students and teachers have the permission view_grade
, but the page will have some different content depending on the user type but only the teacher should access the View Grading
page. Another example, is that on the menu at specific (top/bottom) positions, I would like to display different links depending on the user type.
So I am trying to figure out the best approach to this. Firstly, we need to differentiate between the user types, and a common approach to this are the Profiles. I have reached to the conclusion, that I could utilize the Group model that I already use, which can act as the profile. There is not need to create any extra models since no extra data is needed to be stored for each user type.
So far so good. So I was thinking that I can check if the user belongs to a group, and act accordingly in the template. So either using a template tag or context processor, the check can be performed and the appropriate content can be rendered. However, both the above seem wrong, since they seem inefficient and it would be best to perform the check in the view.
What do you think? Is there a better approach? I am not sure if I am missing something obvious here. To summarize, I would like to have a way to check if the user belongs to a specific group, and act accordingly in the template.
Thank you for reading.