SignupView
Handles social account signup when additional information is required from the user.
Class Definition
class SignupView(
RedirectAuthenticatedUserMixin,
CloseableSignupMixin,
AjaxCapableProcessFormViewMixin,
FormView,
)
Attributes
The form class to use. Defaults to SignupForm.
Template path. Defaults to socialaccount/signup.html or socialaccount/signup.ajax based on ACCOUNT_TEMPLATE_EXTENSION.
The pending social login instance (set during dispatch).
Methods
dispatch()
@method_decorator(login_not_required)
def dispatch(request, *args, **kwargs) -> HttpResponse
Handles the initial request and retrieves the pending social login.
The Django request object.
The HTTP response, or redirect to login if no pending signup.
Behavior:
- Retrieves pending social login from session
- Redirects to login page if no pending signup found
- Proceeds with normal dispatch if signup is pending
def get_form_class() -> type
Returns the form class to use, checking for custom form in settings.
Form class from SOCIALACCOUNT_FORMS['signup'] or default SignupForm.
def get_form_kwargs() -> dict
Returns keyword arguments for instantiating the form.
Dictionary including the sociallogin parameter.
is_open()
Checks if signup is currently open.
True if signup is open, False otherwise.
Delegates to: adapter.is_open_for_signup(request, sociallogin)
def form_valid(form) -> HttpResponse
Called when the form is valid.
The validated form instance.
Redirect response after successful signup.
Process:
- Creates user account with form data
- Connects social account to user
- Logs user in
- Redirects to next URL or default location
get_context_data()
def get_context_data(**kwargs) -> dict
Returns context data for rendering the template.
Context dictionary including site and account.
get_authenticated_redirect_url()
def get_authenticated_redirect_url() -> str
Returns URL to redirect authenticated users to.
URL to the connections page.
URL Configuration
from allauth.socialaccount.views import signup
urlpatterns = [
path('social/signup/', signup, name='socialaccount_signup'),
]
Template Context
The template receives:
form - The signup form
site - The current site
account - The social account being connected
provider - The provider instance
Example template:
{% extends "account/base.html" %}
{% block content %}
<h1>Sign Up with {{ account.get_provider.name }}</h1>
<p>You are about to connect your {{ account.get_provider.name }} account.</p>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign Up</button>
</form>
{% endblock %}
Function-based View
signup = SignupView.as_view()
The module exports a function-based view instance for use in URL configuration.
LoginCancelledView
Displays a page when the user cancels social authentication.
Class Definition
@method_decorator(login_not_required, name="dispatch")
class LoginCancelledView(TemplateView)
Attributes
Template path. Defaults to socialaccount/login_cancelled.html.
Usage
This view is shown when:
- User clicks “Cancel” on the provider’s authorization page
- Provider redirects back with error indicating cancellation
URL Configuration
from allauth.socialaccount.views import login_cancelled
urlpatterns = [
path('social/login/cancelled/', login_cancelled, name='socialaccount_login_cancelled'),
]
Template Example
{% extends "base.html" %}
{% block content %}
<h1>Login Cancelled</h1>
<p>You cancelled the login process.</p>
<p><a href="{% url 'account_login' %}">Try again</a></p>
{% endblock %}
Function-based View
login_cancelled = LoginCancelledView.as_view()
LoginErrorView
Displays an error page when social authentication fails.
Class Definition
class LoginErrorView(TemplateView)
Attributes
Template path. Defaults to socialaccount/authentication_error.html.
Methods
get()
def get(request, *args, **kwargs) -> HttpResponse
Handles GET requests and renders the error page with 401 status.
The Django request object.
HTTP response with 401 Unauthorized status.
Usage
This view is shown when:
- Provider returns an error during authentication
- OAuth flow fails due to invalid state
- Provider denies access
- Token exchange fails
URL Configuration
from allauth.socialaccount.views import login_error
urlpatterns = [
path('social/login/error/', login_error, name='socialaccount_login_error'),
]
Template Example
{% extends "base.html" %}
{% block content %}
<h1>Authentication Error</h1>
<p>An error occurred during the authentication process.</p>
<p>Please try again or contact support if the problem persists.</p>
<p><a href="{% url 'account_login' %}">Back to Login</a></p>
{% endblock %}
Function-based View
login_error = LoginErrorView.as_view()
ConnectionsView
Manages connected social accounts for authenticated users.
Class Definition
@method_decorator(login_required, name="dispatch")
class ConnectionsView(AjaxCapableProcessFormViewMixin, FormView)
Attributes
Template path. Defaults to socialaccount/connections.html.
The form class to use. Defaults to DisconnectForm.
URL to redirect to after successful disconnection. Defaults to socialaccount_connections.
Methods
def get_form_class() -> type
Returns the form class to use, checking for custom form in settings.
Form class from SOCIALACCOUNT_FORMS['disconnect'] or default DisconnectForm.
def get_form_kwargs() -> dict
Returns keyword arguments for instantiating the form.
Dictionary including the request parameter.
def form_valid(form) -> HttpResponse
Called when the form is valid (account disconnected successfully).
The validated form instance.
get_ajax_data()
def get_ajax_data() -> dict
Returns data for AJAX responses.
Dictionary with socialaccounts list containing account data.
Response format:
{
"socialaccounts": [
{
"id": 1,
"provider": "google",
"name": "[email protected]"
},
{
"id": 2,
"provider": "github",
"name": "johndoe"
}
]
}
URL Configuration
from allauth.socialaccount.views import connections
urlpatterns = [
path('social/connections/', connections, name='socialaccount_connections'),
]
Template Context
The template receives:
form - The disconnect form
socialaccounts - QuerySet of user’s social accounts (via form.accounts)
Example template:
{% extends "account/base.html" %}
{% block content %}
<h1>Connected Accounts</h1>
{% if form.accounts %}
<form method="post">
{% csrf_token %}
<ul class="social-accounts">
{% for account in form.accounts %}
<li>
<label>
<input type="radio" name="account" value="{{ account.id }}">
<strong>{{ account.get_provider.name }}</strong>
{{ account }}
{% if account.get_avatar_url %}
<img src="{{ account.get_avatar_url }}" alt="Avatar">
{% endif %}
</label>
</li>
{% endfor %}
</ul>
<button type="submit">Disconnect Selected Account</button>
</form>
{% else %}
<p>No social accounts connected.</p>
{% endif %}
<h2>Connect New Account</h2>
<ul>
{% for provider in providers %}
<li>
<a href="{% provider_login_url provider.id %}">
Connect {{ provider.name }}
</a>
</li>
{% endfor %}
</ul>
{% endblock %}
AJAX Usage
// Get connected accounts
fetch('/accounts/social/connections/', {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(data => {
console.log(data.socialaccounts);
});
// Disconnect account
fetch('/accounts/social/connections/', {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRFToken': getCookie('csrftoken'),
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'account=1'
})
.then(response => response.json())
.then(data => {
console.log('Account disconnected');
});
Function-based View
connections = ConnectionsView.as_view()
View Customization
All views can be extended or replaced:
Extending a View
from allauth.socialaccount.views import SignupView
from django.shortcuts import redirect
class MySignupView(SignupView):
template_name = 'myapp/social_signup.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['custom_data'] = 'value'
return context
def form_valid(self, form):
response = super().form_valid(form)
# Additional processing
send_welcome_email(form.user)
return response
# urls.py
urlpatterns = [
path('social/signup/', MySignupView.as_view(), name='socialaccount_signup'),
]
Adding Custom Views
from django.views.generic import TemplateView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from allauth.socialaccount.models import SocialAccount
@method_decorator(login_required, name='dispatch')
class SocialAccountsAPIView(TemplateView):
def get(self, request, *args, **kwargs):
accounts = SocialAccount.objects.filter(user=request.user)
data = [
{
'provider': account.provider,
'uid': account.uid,
'profile_url': account.get_profile_url(),
'avatar_url': account.get_avatar_url(),
}
for account in accounts
]
return JsonResponse({'accounts': data})