pythondjangogoogle-oauthdjango-allauthdj-rest-auth

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'

I have this error coming from url.py which is weird as I am following this guide https://dj-rest-auth.readthedocs.io/en/latest/installation.html on the Google section. I am pretty sure I have all the relevant settings and I copied the codes for url.py and GoogleLogin.py. Does anyone have any clues on how to deal with this issue?

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 code fragments

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'
        }
    }   
}


......




Solution

  • I have solved this By doing

    GoogleLogin.GoogleLogin.as_view()
    

    instead.

    And renamed GoogleLogin class to GoogleLoginView instead for less confusion.