djagno重写authenticate实现帐号和邮箱的多方式登录。

1、在users应用的view中重写authenticate:

from django.contrib.auth.backends import ModelBackend
from django.db.models import Q from .models import UserProfile 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

2、在settings中进行声明:

AUTHENTICATION_BACKENDS = (
'users.views.CustomBackend',
)
 
上一篇:ubuntu14.04 允许root用ssh登录


下一篇:使用条件注释判断 IE 浏览器版本