Prerequisite: Django-allauth setup and Configuration
Let’s deal with customizing django-allauth signup forms, intervening in registration flow to add custom processes and validations.
Extending Signup Form or adding custom fields in django-allauth:
One of the most common queries about allauth is about adding additional fields or custom fields to the signup form. You can extend the SignupForm class from allauth.account.forms. All you need to do is create a custom class pass the SignupForm to the custom class and define the custom fields and save it. It’s vital to return the user object as it will be passed to other modules for validations. You also need to include a variable in settings.py.
Let’s see this using an example forms.py
In the above snippet CustomSignupForm is extended the class which inherits all the features of SignupForm class and adds the necessary features. Here custom fields by the name first_nameand last_name are created and saved in using the signup module in the same class.
The ACCOUNT_FORMS in settings.py for the above code is
Any other custom forms created can be extended in ACCOUNT_FORMS. The list is illustrated in the documentation.
Similarly, LoginForm UserForm AddEmailForm and others can be extended. However, remember that when you extend these forms and link them in settings.py. Don’t forget to pass the original form (For example SignupForm) as a parameter to your class and sometimes you might have to handle custom validations and return users or some other value. Please refer to the source code to follow the correct flow.
User Intervention and Custom validations:
Let’s discuss adding custom validations and intervening in the user registration flow. DefaultAccountAdapter is very useful and indeed can be used to solve most of the customization problems that a user might encounter while using django–allauth.
Example #1: Restricted List of email’s
After figuring out a way to store and fetch the restricted list, you can use the adapters and raise validation errors in the registration form when a restricted email tries to register. Extend a DefaultAccountAdapter and override the clean_email method. Create an adapter.py in your project directory and extend the default adapter class.
Finally, point the account adapter in settings.py to your extended class.
Example #2: Add a Maximum length to a username
As ACCOUNT_USERNAME_MAX_LENGTH doesn’t exist in the allauth library, DefaultAccountAdaptercan is used to achieve this feature without much pain. Extend the DefaultAccountAdapterclass and overriding the clean_username method. You need to also reference the clean_username once again after our custom validation to complete other inbuilt validations.
The last sentence in the above paragraph is the key to work with DefaultAccountAdapter. You should never forget to reference the original module name for the module to complete other validations.
Finally, point to the subclass in your settings.py
ACCOUNT_ADAPTER = 'YourProject.adapter.UsernameMaxAdapter'
You can refer to the adapters.py file in the source code and extend other modules and add a process or change their flow. The modules such as populate_username, clean_password (which can be customized to restrict commonly used passwords) can have the custom process and flow without rewriting them.
DefaultAccountAdapter can be a potent tool if used in the right circumstances to intervene in allauth’s default process. allauth comes with a vast variety of inbuilt settings, and they are here. You can also download the entire source code from the link in the references below.
stack overflow thread on Example 1
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Extending a list in Python (5 different ways)
- Creating custom user model API extending AbstractUser in Django
- Important differences between Python 2.x and Python 3.x with examples
- Creating and updating PowerPoint Presentations in Python using python - pptx
- Loops and Control Statements (continue, break and pass) in Python
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python | Using variable outside and inside the class and method
- Releasing GIL and mixing threads from C and Python
- Python | Boolean List AND and OR operations
- Difference between 'and' and '&' in Python
- Replace the column contains the values 'yes' and 'no' with True and False In Python-Pandas
- Ceil and floor of the dataframe in Pandas Python – Round up and Truncate
- Login Application and Validating info using Kivy GUI and Pandas in Python
- Get the city, state, and country names from latitude and longitude using Python
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.