Django allauth Google 登录问题

问题描述 投票:0回答:0

我正在尝试使用谷歌登录来完成我需要做的项目,试图找到类似的东西但找不到。问题是当我尝试使用 google 登录时,它会将我重定向到 http://127.0.0.1:8000/accounts/google/login/ 并且没有显示用于选择我要登录的帐户的框.我正在使用自定义用户和表单。

设置.py

INSTALLED_APPS = [
    'WebFinalExam.base',

    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates']
        ,
        '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',
                'django.template.context_processors.request',
            ],
        },
    },
]

AUTH_USER_MODEL = 'base.AppUser'
LOGIN_URL = reverse_lazy('sign-in')
LOGIN_REDIRECT_URL = reverse_lazy('index')

LOGOUT_REDIRECT_URL = reverse_lazy('index')

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
]

SITE_ID = 1
SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
        },
        'OAUTH_PKCE_ENABLED': True,
    }
}
ACCOUNT_FORMS = {
    'login': 'WebFinalExam.base.forms.SignInForm',
    'signup': 'WebFinalExam.base.forms.RegisterForm'
}
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'email'
ACCOUNT_LOGOUT_REDIRECT_URL = 'account_login'

项目网址.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('WebFinalExam.base.urls')),
    path('forum/', include('WebFinalExam.forum.urls')),
    path('accounts/', include('allauth.urls')),
]

应用程序网址.py

urlpatterns = [
    path('', views.HomePage.as_view(), name='index'),
    path('sign-up/', views.SignUp.as_view(), name='sign-up'),
    path('sign-in/', views.SignIn.as_view(), name='sign-in'),
    path('sign-out/', views.SignOut.as_view(), name='sign-out'),
]

模型.py

class AppUser(auth_models.AbstractBaseUser, auth_models.PermissionsMixin):
    email = models.EmailField(
        unique=True,
        null=False,
        blank=False
    )

    is_staff = models.BooleanField(
        default=False,
    )
    is_active = models.BooleanField(
        default=True,
    )
    date_joined = models.DateTimeField(default=timezone.now)

    USERNAME_FIELD = 'email'

    objects = AppUserManager()

    def __str__(self):
        name_from_email = self.email.split('@')[0]
        return name_from_email

表格.py

UserModel = get_user_model()


class SignInForm(auth_forms.AuthenticationForm):
    class Meta:
        model = UserModel
        fields = ('email', 'password')

views.py

class SignIn(auth_views.LoginView):
    form_class = SignInForm
    template_name = 'account/login.html'
    redirect_authenticated_user = True

模板/账户/登录

{% extends 'base.html' %}
{% load static %}
{% load socialaccount %}
{% block links %}
    <link rel="stylesheet" href="{% static 'css/base/sign-in.css' %}">
{% endblock %}

{% block content %}
    <div class="limiter">
        <div class="wrapper">
            <div class="inner-wrapper">
                <form action="{% url 'sign-in' %}" method="post">
                    <div class="form-title">
                        <span>Sign In With</span>
                    </div>
                    <div class="socials">
                        <a href=""><i class="fa-brands fa-square-facebook"></i>Facebook</a>
                        <a href="{% provider_login_url 'google' %}"><i class="fa-brands fa-google"></i>Google</a>
                    </div>
                    {% for field in form %}
                        <div class="form-field">
                            <label>{{ field.label }}</label>
                            {{ field }}
                        </div>
                    {% endfor %}
                    <div class="form-buttons">
                            <button class="sign-in-btn">Sign In</button>
                            <p>Don’t have an account? <a class="sign-up" href="{% url 'sign-up' %}">Sign up</a></p>
                    </div>
                    {% csrf_token %}
                </form>
            </div>
        </div>
    </div>
{% endblock %}

它确实使用谷歌帐户登录但没有重定向我选择我想登录的帐户。

这是我在 url 上尝试使用 google 登录时看到的内容 http://127.0.0.1:8000/accounts/google/login/ (https://i.stack.imgur.com/mNAd6.png)

django google-signin
© www.soinside.com 2019 - 2024. All rights reserved.