私がしたことは:
から継承するクラスを定義しますSocialAuthExceptionMiddleware
メソッドを実装しprocess_exception、
実装されたクラスをMIDDLEWAREリストに追加しsettings.pyます。
で、これmiddleware.pyはアプリのディレクトリ、つまりviews.pyアプリに関連付けられたファイルの同じディレクトリにあるはずです。次のクラスを定義します。
from django.shortcuts import redirect
from django.urls import reverse
from social_core.exceptions import AuthAlreadyAssociated
class FacebookAuthAlreadyAssociatedMiddleware(SocialAuthExceptionMiddleware):
"""Redirect users to desired-url when AuthAlreadyAssociated exception occurs."""
def process_exception(self, request, exception):
if isinstance(exception, AuthAlreadyAssociated):
if request.backend.name == "facebook":
message = "This facebook account is already in use."
if message in str(exception):
# Add logic if required
# User is redirected to any url you want
# in this case to "app_name:url_name"
return redirect(reverse("app_name:url_name"))
でsettings.py、実装されたクラスをMIDDLEWAREリストに追加します。
MIDDLEWARE = [
# Some Django middlewares
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"social_django.middleware.SocialAuthExceptionMiddleware",
# the middleware you just implemented
"app_name.middleware.FacebookAuthAlreadyAssociatedMiddleware",
]
これで問題は解決し、AuthAlreadyAssociated例外が発生したときの制御フローを処理することができました。