Author's profile

Profile Picture
abdulla | Abdulla Fajal

Graduation

India

Followers: (55) | Following: (45)

My name is Abdulla Fajal and I am the owner of "espere.in", if you want to give some suggestions, you can message me.



Message



View author's full profile
Advertisement

Login With Email Instead Of User Name In Django Admin

Oct. 26, 2022


0
1 min read
1.10K


Login With Email Instead Of Username In Django Admin

In this tutorial, we are going to learn how to log in using email instead of the username in Django admin. It's easy, you can use it on the frontend too

let's start:

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.


class User(AbstractUser):
    USERNAME_FIELD = "email"
    email = models.EmailField(max_length=255, unique=True)
    REQUIRED_FIELDS = [
        "username",
    ]

Don’t forget to point AUTH_USER_MODEL to it.

settings.py

AUTH_USER_MODEL = "app_name.User"

Now register the model in the app’s admin.py:

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User

admin.site.register(User, UserAdmin)

You need to run the makemigrations command

python manage.py makemigrations

then have to run migrate command

python manage.py migrate

Then you have to create a superuser

python manage.py createsuperuser

For more information see Django docs

 

django Email-Login Authentication Thanks For reading

Add a comment


Note: If you use these tags, write your text inside the HTML tag.
Login Required