pip install django-allauth pip install alipay-sdk-python python INSTALLED_APPS = [ ... 'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', ... ] python AUTHENTICATION_BACKENDS = [ ... 'allauth.account.auth_backends.AuthenticationBackend', ... ] python LOGIN_REDIRECT_URL = '/' python SOCIALACCOUNT_PROVIDERS = { 'alipay': { 'APP_ID': 'Your Alipay APP ID', 'PUBLIC_KEY': 'Your Alipay Public Key', 'PRIVATE_KEY': 'Your Alipay Private Key', 'SCOPE': ['auth_user'], } } python manage.py makemigrations python manage.py migrate python from allauth.socialaccount.models import SocialAccount from django.views.generic import TemplateView class AlipayLoginView(TemplateView): template_name = 'alipay_login.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['alipay_login_url'] = self.get_login_url() return context def get_login_url(self): provider = 'alipay' callback_url = self.request.build_absolute_uri('/alipay/login/callback') return SocialAccount.providers.get(provider).get_login_url(request=self.request, callback=callback_url) python from allauth.socialaccount.models import SocialLogin from django.views.generic import RedirectView class AlipayLoginCallbackView(RedirectView): pattern_name = 'profile' def get_redirect_url(self, *args, **kwargs): social_login = self.get_social_login() user = social_login.user return super().get_redirect_url(*args, **kwargs) def get_social_login(self): social_login = None try: social_login = self.request.user.socialaccount_set.filter(provider='alipay')[0] except IndexError: pass return social_login html python from django.urls import path from your_app.views import AlipayLoginView, AlipayLoginCallbackView urlpatterns = [ ... path('alipay/login', AlipayLoginView.as_view(), name='alipay_login'), path('alipay/login/callback', AlipayLoginCallbackView.as_view(), name='alipay_login_callback'), ... ]


上一篇:
下一篇:
切换中文