[Fixed]-Django HttpResponseRedirect not working in custom permission (authentication.py)

1👍

Hey I found the solution. It would be helpful if anyone needs this in future. I created an exception handler for rest framework exceptions and initiated in settings.py file:

REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'app_name.exception.function_name'
}

I created my exception handler function custom_exception_handler in exception.py :

from rest_framework.views import exception_handler
from django.http import HttpResponseRedirect

def custom_exception_handler(exc, context):
    response = exception_handler(exc, context)
    # Now add the HTTP status code to the response.
    if response is not None and response.status_code == 403:
        return HttpResponseRedirect("/")
else:
    return response

Leave a comment