Sử dụng django-allauth để thêm đăng nhập Google vào trang web Django.
Các bước
Tạo project trên Google Cloud Console, lấy Client ID & Secret, cấu hình redirect URI:
https://127.0.0.1:8000/accounts/google/login/callback/pip install django-allauthSettings.py: thêm
'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', 'allauth.socialaccount.providers.google', setSITE_ID=1,AUTHENTICATION_BACKENDS.URLs:
path('accounts/', include('allauth.urls'))Admin: Sites → thêm site; Social Applications → thêm Google, điền Client ID/Secret, chọn site.
Template:
{% load socialaccount %}<a href="{% provider_login_url 'google' %}">Login with Google</a>
Kết quả
Nút "Login with Google" → chuyển trang OAuth Google → quay lại đã đăng nhập.