Hi I am creating a custom middleware in Django for the DRF.
So that when an user try to access any of the api the middleware will perform some operation and determine if the user is authorized to access the endpoint or not.
My code is like below:
class PermissionMiddleware(MiddlewareMixin):
def process_view(self, request, view_func, view_args, view_kwargs):
if request.path.startswith('/admin/'):
return None
if request.path.startswith('/api/'):
is_allowed = True
if not is_allowed:
return # < -- What needs to return to block the access
return None
My problem is what should I return from the method for disallowing access to api? I can return None, if I want to give access. But I want to disallow access and return some message from the api view so that user knows that he is not allwed.
So in summery:
- What should I return from the middleware to block access?
- How can I return message to user from the view that he is not authorized?
Thanks