AttributeError: module 'BackendApp.views.GoogleLogin' has no attribute 'as_view'

AttributeError: module 'BackendApp.views.GoogleLogin' has no attribute 'as_view'

path('dj-rest-auth/google/', GoogleLogin.as_view(), name='google_login'),
AttributeError: module 'BackendApp.views.GoogleLogin' has no attribute 'as_view'

我有这个错误来自 url.py,这很奇怪,因为我在 Google 部分遵循本指南 https://dj-rest-auth.readthedocs.io/en/latest/installation.html。我很确定我拥有所有相关设置,并且我复制了 url.pyGoogleLogin.py 的代码。有没有人知道如何处理这个问题?

url.py

from django.contrib import admin
from django.urls import path
from django.urls import include, re_path
from django.conf.urls.static import static
from django.conf import settings
from dj_rest_auth.views import PasswordResetConfirmView, PasswordResetView
from django.views.generic import TemplateView
from .router import router
from BackendApp.views import P2PListingModule, empty_view, GoogleLogin
urlpatterns = [ 
    path('admin/', admin.site.urls),
    path('dj-rest-auth/', include('dj_rest_auth.urls')),
    path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
    path('entity/', include(router.urls)),
    path('enduser/<str:pk>/service/', P2PListingModule.userServiceListing),
    path('enduser/<str:pk>/request/', P2PListingModule.userRequestListing),
    path('enduser/<str:pk>/swap/', P2PListingModule.userSwapListing),
    path('enduser/<str:pk>/premade', P2PListingModule.userPremadeListing),
    path('entity/p2p_listing/order/', P2PListingModule.placeOrder),
    path('api/p2plisting/service', P2PListingModule.ServiceListingView.as_view()),
    path('api/p2plisting/request', P2PListingModule.RequestListingView.as_view()),
    path('api/p2plisting/swap', P2PListingModule.SwapListingView.as_view()),
    path('api/p2plisting/premade', P2PListingModule.PremadeListingView.as_view()),
    re_path(r'^', include('django.contrib.auth.urls')),
    path('dj-rest-auth/password/reset/', PasswordResetView.as_view(), name="rest_password_reset"),
    path(
        "dj-rest-auth/password/reset/confirm/",
        PasswordResetConfirmView.as_view(),
        name="rest_password_reset_confirm",
    ),  
    # path(
   # "/dj-rest-auth/password/reset/confirm/<uidb64>/<token>/",
    #     PasswordResetConfirmView.as_view(),
    #     )
    path('dj-rest-auth/google/', GoogleLogin.as_view(), name='google_login'),
    path('accounts/', include('allauth.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# urlpatterns = [
#     url(r'^admin/', include(admin.site.urls)),
# ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

BackendApp.views.GoogleLogin.py

from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from dj_rest_auth.registration.views import SocialLoginView

class GoogleLogin(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter
    #I do not know if this client id is the callback_url that this requires. -Rya
    callback_url = "http://localhost:8000/accounts/google/login/callbaccontent.comk"
    client_class = OAuth2Client

settings.py 代码片段

from pathlib import Path
# from Backend.custom_dj_rest_auth_serializers import LoginSerializer, UserDetailsSerializer, RegisterSerializer

.....

REST_FRAMEWORK = { 
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],  
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ]   
}

# Application definition

INSTALLED_APPS = [ 
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'BackendApp',
    'rest_framework',
    'rest_framework.authtoken',
    'dj_rest_auth',
    'dj_rest_auth.registration',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google'
]

.....

ROOT_URLCONF = 'Backend.urls'

TEMPLATES = [ 
    {   
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [], 
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },  
]
REST_AUTH_SERIALIZERS = { 
    'LOGIN_SERIALIZER': 'BackendApp.auth_serializers.LoginSerializer',
    'TOKEN_SERIALIZER': 'dj_rest_auth.serializers.TokenSerializer',
    "PASSWORD_RESET_SERIALIZER": "BackendApp.auth_serializers.PasswordResetSerializer"
}
REST_AUTH_REGISTER_SERIALIZERS = { 
    'REGISTER_SERIALIZER': 'BackendApp.auth_serializers.RegisterSerializer'
}
# Provider specific settings
SOCIALACCOUNT_PROVIDERS = { 
    'google': {
        # For each OAuth based provider, either add a ``SocialApp``
        # (``socialaccount`` app) containing the required client
        # credentials, or list them here:
        'APP': {
            'client_id': '986666561005-49aa5ralo3ro80dh1tfnh6gjgcpuulvp.apps.googleusercontent.com',
            'secret':'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',
            'key': 'AIzaSyCSZFYhEM4ZGUUagVsfBB_mwdHjp8t1vWw'
        }
    }   
}


......



我已经通过

解决了这个问题
GoogleLogin.GoogleLogin.as_view()

相反。

并将 GoogleLogin class 重命名为 GoogleLoginView 以减少混淆。