Categories
Python Answers

How to add login with email with Python Django?

Spread the love

To add login with email with Python Django, we can add an EmaolFoeld with unique set to True.

For instance, we write

class MyUser(AbstractUser):
    USERNAME_FIELD = 'email'
    email = models.EmailField(_('email address'), unique=True)
    REQUIRED_FIELDS = []

to create the email EmailField that has unique set to True to make all the values of it unique.

And then we set USERNAME_FIELD to 'email' to make the username field the email field we just created.

Then we can use authenticate(email=email, password=password) to authenticate.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *