Removing Case Sensitivity From Email In Django Login Form
Solution 1:
A cleaner approach might be to override the model field itself if you don't mind losing the formatting of how the user entered their email.
This worked better for me because I only had to change it in one place. Otherwise, you might have to worry about Signup, Login, User Update, API views, etc.
The only method you will have to overwrite is to_python
. This will just lowercase anything being saved to the UserModel.email
field.
from django.db import models
classLowercaseEmailField(models.EmailField):
"""
Override EmailField to convert emails to lowercase before saving.
"""defto_python(self, value):
"""
Convert email to lowercase.
"""
value = super(LowercaseEmailField, self).to_python(value)
# Value can be None so check that it's a string before lowercasing.ifisinstance(value, str):
return value.lower()
return value
Your user model would then just be..
# Assuming you saved the above in the same directory in a file called model_fields.pyfrom .model_fields import LowercaseEmailField
classUserModel(AbstractBaseUser, PermissionsMixin):
email = LowercaseEmailField(unique=True)
# other stuff...
Solution 2:
You don't need to change much to accomplish this - in your case you just need to change the form and make use of Django's built-in form data cleaners or by making a custom field.
You should use the EmailField
instead of a CharField
for built-in validation. Also you did not post your AuthenticationForm, but i presume you have changed it to include email
instead of username
.
With data cleaners:
classSignUpForm(UserCreationForm):
# your code
email = forms.EmailField(required=True)
defclean_email(self):
data = self.cleaned_data['email']
return data.lower()
classAuthenticationForm(forms.Form):
# your code
email = forms.EmailField(required=True)
defclean_email(self):
data = self.cleaned_data['email']
return data.lower()
With a custom field:
classEmailLowerField(forms.EmailField):
defto_python(self, value):
return value.lower()
classSignUpForm(UserCreationForm):
# your code
email = EmailLowerField(required=True)
classAuthenticationForm(forms.Form):
# your code
email = EmailLowerField(required=True)
This way you can make sure that each email is saved to your database in lowercase and that for each login attempt the email is lowercased before compared to a database value.
Solution 3:
When creating the account entry, interpret the email as email.lower()
to create a normalised entry in your database.
When parsing the email/username from a log in form, you should also use email.lower()
to match the database entry.
Note: the normalize_email
only sanitises the domain portion of an email address:
classmethod normalize_email(email)
Normalizes email addresses by lowercasing the domain portion of the email address.
Solution 4:
For anyone using Postgres and django 2 or higher:
There is a CIEmailField
that stores and retrieves the email address case sensitive but compares it case insensitive. This has the benefit of preserving case in the address (should a user want their address to look like JohnDoe@ImportantCompany.com
) without allowing multiple accounts with the same email.
Usage is simple:
from django.contrib.postgres.fields import CIEmailField
# ...classUser(AbstractBaseUser, ...):
email = CIEmailField(unique=True, ...)
# ...
Except on migrating I got the error:
django.db.utils.ProgrammingError: type"citext" does not exist
To solve this, follow this so answer and set up the CITextExtension
before the first CreateModel
migration operation:
from django.contrib.postgres.operations import CITextExtension
classMigration(migrations.Migration):
...
operations = [
CITextExtension(),
...
]
Note: both in the so answer and in the official documentation is stated that you should add it before the first CreateModel migration operation. For me it was sufficient to add it in the first migration that uses the CIEmailField
.
Post a Comment for "Removing Case Sensitivity From Email In Django Login Form"