pip install django-allauth python INSTALLED_APPS = [ ... 'allauth', 'allauth.account', 'allauth.socialaccount', ... ] python AUTHENTICATION_BACKENDS = [ 'django.contrib.auth.backends.ModelBackend', 'allauth.account.auth_backends.AuthenticationBackend', ] python LOGIN_REDIRECT_URL = '/' ACCOUNT_LOGOUT_REDIRECT_URL = '/' SOCIALACCOUNT_QUERY_EMAIL = True SOCIALACCOUNT_AUTO_SIGNUP = True SOCIALACCOUNT_PROVIDERS = { 'google': { 'SCOPE': [ 'profile', 'email', ], 'AUTH_PARAMS': { 'access_type': 'online', } }, 'facebook': { 'SCOPE': [ 'email', ], 'AUTH_PARAMS': { 'auth_type': 'reauthenticate', } } } python manage.py migrate python from django.urls import include ... urlpatterns = [ ... path('accounts/', include('allauth.urls')), ... ] python from django.shortcuts import render from django.contrib.auth.decorators import login_required @login_required def login_view(request): return render(request, 'login.html') python from django.shortcuts import redirect from django.contrib.auth.decorators import login_required @login_required def logout_view(request): return redirect('/') html {% extends 'base.html' %} {% block content %} <h1>Login</h1> <form action="{% url 'account_login' %}" method="POST"> {% csrf_token %} {{ form.as_p }} <button type="submit">Login</button> </form> {% endblock %} python from yourapp.views import login_view, logout_view ... urlpatterns = [ ... path('login/', login_view, name='login'), path('logout/', logout_view, name='logout'), ... ] python from allauth.socialaccount.models import SocialApp google = SocialApp.objects.create( provider='google', name='Google', client_id='YOUR_GOOGLE_CLIENT_ID', secret='YOUR_GOOGLE_SECRET' ) python SOCIALACCOUNT_PROVIDERS = { 'google': { 'SCOPE': [ 'profile', 'email', ], 'AUTH_PARAMS': { 'access_type': 'online', } }, 'facebook': { 'SCOPE': [ 'email', ], 'AUTH_PARAMS': { 'auth_type': 'reauthenticate', } }, 'your_provider': { } }


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