Skip to content

Instantly share code, notes, and snippets.

@Sheikh2Imran
Last active December 21, 2021 14:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sheikh2Imran/89dbafc0f464b494477930f60b7e7356 to your computer and use it in GitHub Desktop.
Save Sheikh2Imran/89dbafc0f464b494477930f60b7e7356 to your computer and use it in GitHub Desktop.
Custom method decorator in Django
def authentication_decorator(function):
def wrap(request, *args, **kwargs):
user_token = request.META.get('HTTP_AUTHORIZATION')
user_response, status_code = backend_obj.get_user(user_token)
if status_code == 200:
kwargs = user_response
return function(request, *args, **kwargs)
return wrap
from django.views import View
from django.http import HttpResponse
from django.utils.decorators import authentication_decorator
from .decorators import authentication_decorator
class IntroductionView(View):
message = "Have a good day sir"
@method_decorator(authentication_decorator)
def get(self, request):
return HttpResponse(self.greeting)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment