Form for completing social account signup when additional information is required.
Inheritance
class SignupForm(BaseSignupForm)
Extends allauth.account.forms.BaseSignupForm with social account-specific functionality.
Constructor
def __init__(self, *args, **kwargs)
The SocialLogin instance for the user being signed up. Must be passed as a keyword argument.
Whether email is required. Defaults to SOCIALACCOUNT_EMAIL_REQUIRED setting.
Example:
from allauth.socialaccount.forms import SignupForm
form = SignupForm(sociallogin=sociallogin, data=request.POST)
if form.is_valid():
user = form.save(request)
Attributes
The social login instance associated with this signup.
Methods
save()
def save(request) -> User
Saves the user account with data from both the form and the social login.
The Django request object.
The newly created user instance.
Example:
if form.is_valid():
user = form.save(request)
# User is now saved with social account connected
Process:
- Calls the adapter’s
save_user() method
- Invokes
custom_signup() hook for additional processing
- Returns the saved user
validate_unique_email()
def validate_unique_email(value) -> str
Validates that the email address is unique.
The email address to validate.
The validated email address.
Raises:
ValidationError if the email is already taken, with a provider-specific error message.
Example:
# Automatically called during form validation
# Error message will include the provider name:
# "An account already exists with this email address.
# Please sign in to that account first, then connect your Google account."
Usage in Views
from django.views.generic.edit import FormView
from allauth.socialaccount.forms import SignupForm
class SocialSignupView(FormView):
form_class = SignupForm
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['sociallogin'] = self.get_sociallogin()
return kwargs
def form_valid(self, form):
user = form.save(self.request)
# Continue with login flow
return HttpResponseRedirect(self.get_success_url())
Form for disconnecting a social account from a user’s profile.
Inheritance
class DisconnectForm(forms.Form)
Constructor
def __init__(self, *args, **kwargs)
The Django request object. Must be passed as a keyword argument.
Example:
from allauth.socialaccount.forms import DisconnectForm
form = DisconnectForm(request=request, data=request.POST)
if form.is_valid():
form.save()
Fields
Radio select field for choosing which social account to disconnect.Widget: RadioSelect
Required: Yes
Queryset: Social accounts belonging to the current user
Attributes
The Django request object.
QuerySet of the user’s social accounts.
Methods
clean()
Validates that the selected account can be safely disconnected.
Raises:
ValidationError if disconnecting the account would leave the user unable to log in.
Validation checks:
- User has a usable password, OR
- User has another connected social account, OR
- User has a verified email address
Example error:
# If user only has this social account and no password:
# "You cannot disconnect this account as you have no password set.
# Please set a password first."
save()
Disconnects the selected social account from the user.
Example:
if form.is_valid():
form.save()
# Social account has been disconnected
# User receives notification email
Process:
- Validates the disconnection is safe
- Deletes the social account
- Sends
social_account_removed signal
- Sends notification email to user
Usage in Views
from django.views.generic.edit import FormView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from allauth.socialaccount.forms import DisconnectForm
@method_decorator(login_required, name='dispatch')
class ConnectionsView(FormView):
form_class = DisconnectForm
template_name = 'socialaccount/connections.html'
success_url = '/accounts/social/connections/'
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['request'] = self.request
return kwargs
def form_valid(self, form):
form.save()
messages.success(self.request, 'Social account disconnected.')
return super().form_valid(form)
Template Usage
<form method="post">
{% csrf_token %}
<h3>Connected Accounts</h3>
{% for account in form.accounts %}
<div class="social-account">
<label>
<input type="radio" name="account" value="{{ account.id }}">
{{ account.get_provider.name }} - {{ account }}
</label>
</div>
{% endfor %}
<button type="submit">Disconnect</button>
</form>
Both forms can be customized through settings:
# settings.py
SOCIALACCOUNT_FORMS = {
'signup': 'myapp.forms.MyCustomSignupForm',
'disconnect': 'myapp.forms.MyCustomDisconnectForm',
}
from allauth.socialaccount.forms import SignupForm
class MyCustomSignupForm(SignupForm):
newsletter = forms.BooleanField(
required=False,
label='Subscribe to newsletter'
)
def save(self, request):
user = super().save(request)
if self.cleaned_data.get('newsletter'):
# Subscribe user to newsletter
subscribe_to_newsletter(user.email)
return user
from allauth.socialaccount.forms import DisconnectForm
class MyCustomDisconnectForm(DisconnectForm):
confirm = forms.BooleanField(
required=True,
label='I understand this will disconnect my social account'
)
def clean(self):
cleaned_data = super().clean()
if not cleaned_data.get('confirm'):
raise forms.ValidationError(
'You must confirm the disconnection.'
)
return cleaned_data