Reputation: 104
Using class based views. I have a decorator that retrieves headers for verification. However I get this error accessing the request headers in the decorator:
Decorator exception 'DetailsView' object has no attribute 'headers'
I must emphasize that accessing request headers in the view function works fine.
View function:
class DetailsView(View):
@check_access
def get(self, request):
res = {
'status': 200,
'response': 'heeaders test.',
'test': 'okay'
}
return HttpResponse(JsonResponse(res, safe=False), content_type='text/json')
Decorator:
def check_access():
def decorator(view_function):
def wrap(request, *args, **kwargs):
try:
print("headers", request.headers)
return view_function(request, *args, **kwargs)
except Exception as e:
return HttpResponse('Unauthorized', status=401)
return wrap
return decorator
Upvotes: 1
Views: 743
Reputation: 209
Delete 'def check_access():' and Change decorate function to check_access. And move 'self' argument.
in View Function
class DetailsView(View):
@check_access
def get(request): # Delete Self
....
in decorator
def check_access(view_function):
def wrap(self, request, *args, **kwargs): # add self
...
return wrap
referenced site : Decorator
Upvotes: 2