I want to do some extra validation on fields in django-allauth. For example I want to prevent using free email addresses. So I want to run this method on signup
def clean_email(self):
email_domain = self.cleaned_data['email'].split('@')[1]
if email_domain in self.bad_domains:
raise forms.ValidationError(_("Registration using free email addresses is prohibited. Please supply a different email address."))
Similarly I want to run custom validation on different fields other than email address. How can I perform this?
We had a basic introduction to Django's Forms and Model forms in my previous post. We will now discuss in detail the validation of form data. Form validation is the main reason that any developer has to use Forms for. The basic security principle of any computer application is "do not trust the user input".
Follow the URL patterns to display the registration form. Eg: Visit localhost:8000/accounts/login to display the login page. Most django-allauth features are can be configured using the built-in adapters and variables by placing them in the file settings.py.
We can check them in the Django shell which we can start with: Once the shell is up and running, do the following. We have only checked the status of the first_name form field. All the other fields are also similar. There is only one validator associated with the first_name field and that is MaxLengthValidator.
Create a Django project if you already don’t have one. Install django-allauth using the command pip install django-allauth Add, allauthallauth.account, allauth.socialaccount and all the social login features you need to INSTALLED_APPS section in settings.py. You can view the entire list of supported API's here.
There are some adapters on the allauth configuration. For example this one:
ACCOUNT_ADAPTER (="allauth.account.adapter.DefaultAccountAdapter")
Specifies the adapter class to use, allowing you to alter certain default behaviour.
You can specify a new adapter by overriding the default one. Just override the clean_email method.
class MyCoolAdapter(DefaultAccountAdapter):
def clean_email(self, email):
"""
Validates an email value. You can hook into this if you want to
(dynamically) restrict what email addresses can be chosen.
"""
*** here goes your code ***
return email
Then modify the ACCOUNT_ADAPTER on the settings.py
ACCOUNT_ADAPTER = '**app**.MyCoolAdapter'
Check the default behavior on: https://github.com/pennersr/django-allauth/blob/master/allauth/account/adapter.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With