# Email and Username as Authenticator

**URL:** <https://forum.djangoproject.com/t/email-and-username-as-authenticator/2751>\
**Category:** Using Django\
**Created:** [June 2, 2020, 6:55pm UTC](https://forum.djangoproject.com/t/email-and-username-as-authenticator/2751 "2020-06-02T18:55:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muskua](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Muskua](https://forum.djangoproject.com/u/Muskua)\
**Post date:** [June 2, 2020, 6:55pm UTC](https://forum.djangoproject.com/t/email-and-username-as-authenticator/2751/1 "2020-06-02T18:55:40Z")

</div>

Hello Guys, i found some thread online, and i tried more than once and failed, i think its easy overall but i am still locked :S  
Trying to authenticate users with either email or username.  
When i say Users i got a custom user, base user + profile with just a few more info about the user submitted through registration form:

class ProfiloUtente(models.Model):  
user = models.OneToOneField(User, related\_name=‘profile’, on\_delete=models.CASCADE)  
utente = models.ForeignKey(Utente, on\_delete=models.CASCADE)  
studio = models.ManyToManyField(Studio)  
telefono\_fisso = models.CharField(max\_length=20, default=None, blank=True)  
telefono\_mobile = models.CharField(max\_length=20, default=None, blank=True)  
indirizzo = models.CharField(max\_length=40, default=None, blank=True)  
citta = models.CharField(max\_length=50, default=None, blank=True)  
cap = models.CharField(max\_length=5, default=None, blank=True)  
provincia = models.CharField(max\_length=30, default=None, blank=True)  
cod\_fiscale = models.CharField(max\_length=16, default=None, blank=True)  
p\_iva = models.CharField(max\_length=27, default=None, blank=True)  
def **str** (self):  
return self.user.username

And this is my data about the default user fields:

class RegistrationForm(UserCreationForm):  
email = forms.EmailField(required=True)  
first\_name = forms.CharField(max\_length=30, required=True)  
last\_name = forms.CharField(max\_length=100, required=True)

```
class Meta:
    model = User
    fields = (
        'username',
        'email',
        'first_name',
        'last_name',
        'password1',
        'password2',

    )

```

…  
…

The login form is the django default login with username + psw.  
Could someone help me understanding what i could do to extend the field username to also take the email into it?  
Thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 2, 2020, 7:53pm UTC](https://forum.djangoproject.com/t/email-and-username-as-authenticator/2751/2 "2020-06-02T19:53:12Z")

</div>

Are you talking about allowing users to login to the site using either their username or their email address?

If so, my first attempt on this would be to create a custom AuthenticationForm that inherits from the built-in [AuthenticationForm](https://github.com/django/django/blob/06c8565a4650b359bdfa59f9707eaa0d1dfd7223/django/contrib/auth/forms.py#L173), overriding the [clean](https://github.com/django/django/blob/06c8565a4650b359bdfa59f9707eaa0d1dfd7223/django/contrib/auth/forms.py#L210) method to try authenticating the person both ways and allowing them to be logged in if either attempt worked.

Ken

---

<div class="post-metadata">

**Author:** ![Muskua](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Muskua](https://forum.djangoproject.com/u/Muskua)\
**Post date:** [June 3, 2020, 2:02am UTC](https://forum.djangoproject.com/t/email-and-username-as-authenticator/2751/3 "2020-06-03T02:02:37Z")

</div>

@KenWhitesell, ill try it soon and notice you, it should be a fast work.  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Muskua](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Muskua](https://forum.djangoproject.com/u/Muskua)\
**Post date:** [June 3, 2020, 1:53pm UTC](https://forum.djangoproject.com/t/email-and-username-as-authenticator/2751/4 "2020-06-03T13:53:35Z")

</div>

may you help me to get a better understanding of what i should do? on a large scale i could understand but when it comes to practice i don’t know how to move myself through it.  
Ill repost with some code snippets

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 3, 2020, 2:15pm UTC](https://forum.djangoproject.com/t/email-and-username-as-authenticator/2751/5 "2020-06-03T14:15:39Z")

</div>

When you do post code snippets, please insert them between lines containing only 3 backticks - ```. (Make sure you use the backtick - ` and not the apostrophe - '.) That means you would have one line with the 3 backticks, your code, and then one more line with just the 3 backticks. That allows this forum software to format your code nicely:

````auto
# The line above this is just ```
def function(parm):
    return parm
# The line after this is just ```

````

So yes, go ahead and post what you’ve got and we can work forward from there.

Ken

---

<div class="post-metadata">

**Author:** ![Muskua](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Muskua](https://forum.djangoproject.com/u/Muskua)\
**Post date:** [June 3, 2020, 2:19pm UTC](https://forum.djangoproject.com/t/email-and-username-as-authenticator/2751/6 "2020-06-03T14:19:49Z")

</div>

Sry Ken, i should have posted it here, but its a long thread and perhaps could help some other people if the visibility is about a post on it’s own.  
I’ll link it here

> [@Django Username or Email Authenticate](https://forum.djangoproject.com/t/django-username-or-email-authenticate/2775):
>
> i’m struggling a bit with this problem, some says i have to override AuthenticationForm but i don’t know how cause i got a custom user registration and login, i’ll post it: urls.py urlpatterns = [ path('login/', LoginView.as\_view(), {'template\_name': 'accounts/login.html'}, name='login'), Note im using default loginview right now. So in views.py i must have this def login(request): return render(request, '/login.html') ... ... Of course login.html is just like this \<div class='c…

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [June 3, 2020, 2:24pm UTC](https://forum.djangoproject.com/t/email-and-username-as-authenticator/2751/7 "2020-06-03T14:24:25Z")

</div>

Is there a benefit to using the AuthenticationForm approach rather than a custom backend?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 3, 2020, 2:36pm UTC](https://forum.djangoproject.com/t/email-and-username-as-authenticator/2751/8 "2020-06-03T14:36:03Z")

</div>

A _lot_ less work. The custom authentication form ends up being one method in one form consisting of maybe 15 lines of code. (See the linked thread for the continuation of this discussion.)
