Skip to content

Commit

Permalink
邮箱登录
Browse files Browse the repository at this point in the history
  • Loading branch information
kele59 committed Apr 3, 2017
1 parent a621b9e commit 2ebb581
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions apps/users/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
from django.shortcuts import render
from django.contrib.auth import authenticate, login
from django.contrib.auth.backends import ModelBackend
from django.db.models import Q

from .models import UserProfile

# Create your views here.

#让用户可以用邮箱登录
class CustomBackend(ModelBackend):
def authenticate(self, username=None, password=None, **kwargs):
try:
user = UserProfile.objects.get(Q(username = username) | Q(email=username))
if user.check_password(password):
return user
except Exception as e:
return None


def user_login(request):
if request.method == 'POST':
user_name = request.POST.get('username', '')
password = request.POST.get('password', '')
# 上面的 authenticate 方法 return user
user = authenticate(username=user_name, password=password)

if user is not None:
Expand Down
3 changes: 2 additions & 1 deletion imooc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@

ALLOWED_HOSTS = []


# AUTH 方法(支持邮箱登录)
AUTHENTICATION_BACKENDS = ('users.views.CustomBackend',)

# Application definition

Expand Down

0 comments on commit 2ebb581

Please sign in to comment.