Search code examples
pythonapimiddleware

Python middleware call on a particular request


I am implementing custom middleware which will check for the token authentication on every request that made . i want this to run for every request accept one

class CheckAuthorization(object):
    def process_request(self, request):
        getKey = request.POST.get('authKey')
        if getKey is not None and getKey != '':
            try:
                auth = TblAutherization.objects.get(secret_key = request.POST.get('authKey'))
            except TblAutherization.DoesNotExist:
                response = JsonResponse({'Status':'Error','Response code': 107,'Message':'Invalid Request'})
                return HttpResponse(response, content_type='application/json')
        else:
            response = JsonResponse({'Status':'Error','Response code': 105,'Message':'Missing Paramters'})
            return HttpResponse(response, content_type='application/json')

I Want this middleware not to be called for one particular request, hows thats possible. if you can guide me to do the same any other way. i am new to python

Thanks


Solution

  • To ignore the particular url, you can add a check at the beginning of process_request, below is a sample code:

    def process_request(self, request):
        full_path = request.get_full_path()
        if full_path.startswith('/users/login'):
            return