Using Django's Built-in Permissions inside React Template #293
-
Apologies if this is in the docs somewhere - I've not been able to track it down. I'm trying to show or hide some elements in reactive based on specific permissions or roles that the Django user holds. For example, I would do it this way in the Django template language: I can block views entirely (and just throw an error when someone tries to access them), but I can't block just specific elements on the screen. For example, I use this in my menus to just hide menu items that specific users cannot use. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
These are all context processors. Of course, because it's all in the same Python process, Django doesn't need to know in advance what you want in your templates. Fortunately, Reactivated has full support for custom context processors, just make sure they are typed. So in User = Pick[models.User, "is_superuser", "can_delete_polls"]
def user(request: HttpRequest) -> Optional[User]:
return request.user if request.user.is_authenticated else None Note you'll need to implement a property for Then turn on that context processor. You can of course, just type a full dictionary of what exactly you want if you don't want to use Pick. |
Beta Was this translation helpful? Give feedback.
You could follow this guide on creating custom context processors and how to register them in the django settings: https://dev.to/gilbishkosma/custom-context-processors-in-django-3c93
A simple custom processor example is the following: https://github.com/mmonj/Inventory-Manager/blob/reactivated-dev/server/context_processors.py
as for how to use it inside of the react template: https://github.com/mmonj/Inventory-Manager/blob/reactivated-dev/client/components/stockTracker/NavigationBar.tsx