Skip to content

Commit a4d3ad8

Browse files
Copilotnijel
andauthored
Add concise quickstart section to Django documentation (#364)
* Initial plan * Add Django quickstart/tutorial guide to documentation Co-authored-by: nijel <[email protected]> * Update Django docs with concise quickstart section instead of separate guide Co-authored-by: nijel <[email protected]> * Remove confusing common settings examples from quickstart Co-authored-by: nijel <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: nijel <[email protected]>
1 parent 76cdd1b commit a4d3ad8

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

docs/configuration/django.rst

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,75 @@ And for MongoEngine_ ORM::
1818
$ pip install social-auth-app-django-mongoengine
1919

2020

21+
Quickstart
22+
----------
23+
24+
This quickstart covers the essential configuration to get social authentication working in your Django project.
25+
26+
**1. Add to INSTALLED_APPS**::
27+
28+
INSTALLED_APPS = (
29+
...
30+
'social_django',
31+
)
32+
33+
**2. Configure authentication backends** (example for Google OAuth2)::
34+
35+
AUTHENTICATION_BACKENDS = (
36+
'social_core.backends.google.GoogleOAuth2',
37+
'django.contrib.auth.backends.ModelBackend', # Keep for username/password login
38+
)
39+
40+
**3. Add OAuth credentials to settings.py**:
41+
42+
This is where you configure your ``client_id``, ``client_secret``, and ``scope`` for each provider::
43+
44+
# Google OAuth2
45+
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your-client-id.apps.googleusercontent.com'
46+
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'your-client-secret'
47+
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
48+
'https://www.googleapis.com/auth/userinfo.email',
49+
'https://www.googleapis.com/auth/userinfo.profile',
50+
]
51+
52+
For other providers, the pattern is ``SOCIAL_AUTH_<PROVIDER>_KEY``, ``SOCIAL_AUTH_<PROVIDER>_SECRET``, and ``SOCIAL_AUTH_<PROVIDER>_SCOPE``. See :doc:`/backends/index` for provider-specific settings.
53+
54+
.. warning::
55+
Never commit credentials to version control. Use environment variables instead::
56+
57+
import os
58+
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ.get('GOOGLE_OAUTH2_KEY')
59+
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ.get('GOOGLE_OAUTH2_SECRET')
60+
61+
**4. Add URLs to urls.py**::
62+
63+
urlpatterns = [
64+
...
65+
path('', include('social_django.urls', namespace='social')),
66+
]
67+
68+
**5. Configure redirect URLs**::
69+
70+
LOGIN_URL = '/login/'
71+
LOGIN_REDIRECT_URL = '/'
72+
LOGOUT_REDIRECT_URL = '/'
73+
74+
**6. Run migrations**::
75+
76+
python manage.py migrate
77+
78+
**7. Add login link in template**::
79+
80+
<a href="{% url 'social:begin' 'google-oauth2' %}">Login with Google</a>
81+
82+
.. note::
83+
**Database considerations**: SQLite has field length limitations that can cause issues. For production, use PostgreSQL or MySQL. If using MySQL InnoDB or SQLite, add::
84+
85+
SOCIAL_AUTH_UID_LENGTH = 223
86+
87+
For additional configuration options, see :doc:`/configuration/settings`.
88+
89+
2190
Register the application
2291
------------------------
2392

0 commit comments

Comments
 (0)