Django не может реализовать функцию Decorator_from_middleware ⇐ Python
-
Anonymous
Django не может реализовать функцию Decorator_from_middleware
I am working on a project where I am trying to write a middleware which when called will verify token from a third party service and check roles stored in that token. I am able to write and use middleware successfully, but I need to implement it for selected views only, for which I am unable to convert the middleware to decorator using decorator_from_middleware to use only where it is required.
This is the middleware class (with token verification part removed).
class SimpleMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): # Code to be executed for each request before # the view (and later middleware) are called. print(f"inside the custom middleware") response = self.get_response(request) # Code to be executed for each request/response after # the view is called. return response This is how I am trying to use it in a view file.
from django.utils.decorators import decorator_from_middleware from users.middleware import SimpleMiddleware simple_decorator = decorator_from_middleware(SimpleMiddleware) class UserPoolInfo(APIView): @simple_decorator def get(self, request): user_manager_object = UserManager() user_pool_info = user_manager_object.get_user_pool_info() response_data = { "status":"Success", "statusCode": status.HTTP_200_OK, "message":"fetched user pool information", "data": user_pool_info } return Response(response_data, status.HTTP_200_OK) we are using Django 5.0 with Django Rest Framework.
I have tried following answer from this question but it does not seem to work. I have also tried documentation which says that custom middleware should be compatible with old style of middleware which I cannot figure out how to write, as my exposure to django version before 5.0 is non existent.
Источник: https://stackoverflow.com/questions/779 ... ctionality
I am working on a project where I am trying to write a middleware which when called will verify token from a third party service and check roles stored in that token. I am able to write and use middleware successfully, but I need to implement it for selected views only, for which I am unable to convert the middleware to decorator using decorator_from_middleware to use only where it is required.
This is the middleware class (with token verification part removed).
class SimpleMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): # Code to be executed for each request before # the view (and later middleware) are called. print(f"inside the custom middleware") response = self.get_response(request) # Code to be executed for each request/response after # the view is called. return response This is how I am trying to use it in a view file.
from django.utils.decorators import decorator_from_middleware from users.middleware import SimpleMiddleware simple_decorator = decorator_from_middleware(SimpleMiddleware) class UserPoolInfo(APIView): @simple_decorator def get(self, request): user_manager_object = UserManager() user_pool_info = user_manager_object.get_user_pool_info() response_data = { "status":"Success", "statusCode": status.HTTP_200_OK, "message":"fetched user pool information", "data": user_pool_info } return Response(response_data, status.HTTP_200_OK) we are using Django 5.0 with Django Rest Framework.
I have tried following answer from this question but it does not seem to work. I have also tried documentation which says that custom middleware should be compatible with old style of middleware which I cannot figure out how to write, as my exposure to django version before 5.0 is non existent.
Источник: https://stackoverflow.com/questions/779 ... ctionality