Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理Python Social Auth上的异常_Python_Django_Authentication_Social - Fatal编程技术网

如何处理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

如何处理Python Social Auth上的此类异常


我找到的所有答案都是针对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'))