如何处理Python Social Auth上的异常
如何处理Python Social Auth上的此类异常如何处理Python Social Auth上的异常,python,django,authentication,social,Python,Django,Authentication,Social,如何处理Python Social Auth上的此类异常 我找到的所有答案都是针对Django Social Auth的,但自从它们被编写以来,似乎已经发生了很大的变化 您可以在应用程序的middleware.py中创建新的中间件: from social_django.middleware import SocialAuthExceptionMiddleware from social_core import exceptions as social_exceptions from
我找到的所有答案都是针对Django Social Auth的,但自从它们被编写以来,似乎已经发生了很大的变化 您可以在应用程序的
middleware.py
中创建新的中间件:
from social_django.middleware import SocialAuthExceptionMiddleware
from social_core import exceptions as social_exceptions
from django.http import HttpResponse
class MySocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
def process_exception(self, request, exception):
if hasattr(social_exceptions, exception.__class__.__name__):
# Here you can handle the exception as you wish
return HttpResponse("Exception %s while processing your social account." % exception)
else:
return super(MySocialAuthExceptionMiddleware, self).process_exception(request, exception)
并将其路径添加到settings.py
MIDDLEWARE_CLASSES = (
...
'path.to.MySocialAuthExceptionMiddleware',
)
如果假设问题发生在某个地方,但会在此处引发..如果要显式处理AuthAlreadyAssociated异常,请使用以下示例<代码>如果isinstance(异常,社会性\u异常。AuthalReadyaSociated):。和重定向,例如:
返回重定向(反向('index'))
。