File: forms.rst

package info (click to toggle)
django-allauth 0.38.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,612 kB
  • sloc: python: 15,213; xml: 849; makefile: 138
file content (289 lines) | stat: -rw-r--r-- 8,391 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
Forms
=====

The following forms can be overridden as needed.

- Add extra fields for extra required information
- Override save to add extra functionality on save

Overriding Save
---------------

If you decide to add fields to a form, you will need to
manually save the custom fields data.

ACCOUNT_FORMS
=============

Default Settings::

    ACCOUNT_FORMS = {
        'login': 'allauth.account.forms.LoginForm',
        'signup': 'allauth.account.forms.SignupForm',
        'add_email': 'allauth.account.forms.AddEmailForm',
        'change_password': 'allauth.account.forms.ChangePasswordForm',
        'set_password': 'allauth.account.forms.SetPasswordForm',
        'reset_password': 'allauth.account.forms.ResetPasswordForm',
        'reset_password_from_key': 'allauth.account.forms.ResetPasswordKeyForm',
    }

login (``allauth.account.forms.LoginForm``)
-------------------------------------------

Used on `account_login <views.html#login-account-login>`__ view.

``save`` is not called, you need to override ``login``
::

    from allauth.account.forms import LoginForm
    class MyCustomLoginForm(LoginForm):

        def login(self, *args, **kwargs):

            # Add your own processing here.

            # You must return the original result.
            return super(MyCustomLoginForm, self).login(*args, **kwargs)

You have access to the following:

- ``self.user`` is the User object that is logging in.

``settings.py``::

    ACCOUNT_FORMS = {'login': 'mysite.forms.MyCustomLoginForm'}

signup (``allauth.account.forms.SignupForm``)
---------------------------------------------

Used on `account_signup <views.html#signup-account-signup>`__ view.

::

    from allauth.account.forms import SignupForm
    class MyCustomSignupForm(SignupForm):

        def save(self, request):

            # Ensure you call the parent classes save.
            # .save() returns a User object.
            user = super(MyCustomSignupForm, self).save(request)

            # Add your own processing here.

            # You must return the original result.
            return user

``settings.py``::

    ACCOUNT_FORMS = {'signup': 'mysite.forms.MyCustomSignupForm'}

add_email (``allauth.account.forms.AddEmailForm``)
--------------------------------------------------

Used on `account_email <views.html#e-mails-management-account-email>`__ view.

::

    from allauth.account.forms import AddEmailForm
    class MyCustomAddEmailForm(AddEmailForm):

        def save(self):

            # Ensure you call the parent classes save.
            # .save() returns a allauth.account.models.EmailAddress object.
            email_address_obj = super(MyCustomAddEmailForm, self).save()

            # Add your own processing here.

            # You must return the original result.
            return email_address_obj

You have access to the following:

- ``self.user`` is the User object that is logged in.

``settings.py``::

    ACCOUNT_FORMS = {'add_email': 'mysite.forms.MyCustomAddEmailForm'}

change_password (``allauth.account.forms.ChangePasswordForm``)
--------------------------------------------------------------

Used on `account_change_password <views.html#password-management>`__ view.

::

    from allauth.account.forms import ChangePasswordForm
    class MyCustomChangePasswordForm(ChangePasswordForm):

        def save(self):

            # Ensure you call the parent classes save
            # .save() does not return anything
            super(MyCustomChangePasswordForm, self).save()

            # Add your own processing here.

You have access to the following:

- ``self.user`` is the User object that is logged in.

``settings.py``::

    ACCOUNT_FORMS = {'change_password': 'mysite.forms.MyCustomChangePasswordForm'}

set_password (``allauth.account.forms.SetPasswordForm``)
--------------------------------------------------------

Used on `account_set_password <views.html#password-management>`__ view.

::

    from allauth.account.forms import SetPasswordForm
    class MyCustomSetPasswordForm(SetPasswordForm):

        def save(self):

            # Ensure you call the parent classes save
            # .save() does not return anything
            super(MyCustomSetPasswordForm, self).save()

            # Add your own processing here.

You have access to the following:

- ``self.user`` is the User object that is logged in.

``settings.py``::

    ACCOUNT_FORMS = {'set_password': 'mysite.forms.MyCustomSetPasswordForm'}

reset_password (``allauth.account.forms.ResetPasswordForm``)
------------------------------------------------------------

Used on `account_reset_password <views.html#password-reset-account-reset-password>`__ view.

::

    from allauth.account.forms import ResetPasswordForm
    class MyCustomSetPasswordForm(ResetPasswordForm):

        def save(self):

            # Ensure you call the parent classes save
            # .save() returns a string containing the email address supplied
            email_address = super(MyCustomResetPasswordForm, self).save()

            # Add your own processing here.

            # Ensure you return the original result
            return email_address

You have access to the following:

- ``self.users`` is a list of all possible User objects with matching email address

``settings.py``::

    ACCOUNT_FORMS = {'reset_password': 'mysite.forms.MyCustomResetPasswordForm'}

reset_password_from_key (``allauth.account.forms.ResetPasswordKeyForm``)
------------------------------------------------------------------------

Used on `account_reset_password <views.html#password-reset-account-reset-password>`__ view.

::

    from allauth.account.forms import ResetPasswordKeyForm
    class MyCustomResetPasswordKeyForm(ResetPasswordKeyForm):

        def save(self):

            # Add your own processing here.

            # Ensure you call the parent classes save
            # .save() does not return anything
            super(MyCustomResetPasswordKeyForm, self).save()

You have access to the following:

- ``self.user`` is the User object

``settings.py``::

    ACCOUNT_FORMS = {'reset_password_from_key': 'mysite.forms.MyCustomResetPasswordKeyForm'}

SOCIALACCOUNT_FORMS
===================

Default Settings::

    SOCIALACCOUNT_FORMS = {
        'login': 'allauth.socialaccount.forms.DisconnectForm',
        'signup': 'allauth.socialaccount.forms.SignupForm',
    }

signup (``allauth.socialaccount.forms.SignupForm``)
---------------------------------------------------

Used on socialaccount_signup view used when someone initially signs up
with a social account and needs to create an account.

::

    from allauth.socialaccount.forms import SignupForm
    class MyCustomSocialSignupForm(SignupForm):

        def save(self):

            # Ensure you call the parent classes save.
            # .save() returns a User object.
            user = super(MyCustomSocialSignupForm, self).save()

            # Add your own processing here.

            # You must return the original result.
            return user

You have access to the following:

- ``self.socialaccount``

``settings.py``::

    SOCIALACCOUNT_FORMS = {'signup': 'mysite.forms.MyCustomSocialSignupForm'}

disconnect (``allauth.socialaccount.forms.DisconnectForm``)
-----------------------------------------------------------

Used on socialaccount_connections view, used when removing a social account.

::

    from allauth.socialaccount.forms import DisconnectForm
    class MyCustomSocialDisconnectForm(DisconnectForm):

        def save(self):

            # Add your own processing here if you do need access to the
            # socialaccount being deleted.

            # Ensure you call the parent classes save.
            # .save() does not return anything
            super(MyCustomSocialDisconnectForm, self).save()

            # Add your own processing here if you don't need access to the
            # socialaccount being deleted.

You have access to the following:

- ``self.request`` is the request object
- ``self.accounts`` is a list containing all of the users SocialAccount objects.
- ``self.cleaned_data['account']`` contains the socialaccount being deleted. .save()
  issues the delete so if you need access to the socialaccount beforehand, move your
  code before .save()

``settings.py``::

    SOCIALACCOUNT_FORMS = {'disconnect': 'mysite.forms.MyCustomSocialDisconnectForm'}