Log in user using either email address or username in Django

后端 未结 13 2578
故里飘歌
故里飘歌 2020-12-02 08:34

I am trying to create an auth backend to allow my users to log in using either their email address or their username in Django 1.6 with a custom user model. The backend work

13条回答
  •  旧巷少年郎
    2020-12-02 08:55

    I wrote the code for this in 2 simple steps :

    1. VIEWS.py
         if request.method == 'POST':
                userinput = request.POST['username']
    
                try:
                    username = userbase.objects.get(email=userinput).username
                except userbase.DoesNotExist:
                    username = request.POST['username']
                password = request.POST['password']
    
    1. INDEX.html

      I created 2 input fields from which 1st is for username/email. I take whatever input is given and try to search the same data in email column of db, if it matches, I return the username and then try to authenticate , if it doesn't , I use input directly as Username.

    I'm using Django 2.2

提交回复
热议问题