首先创建一个新的login app,用来存放认证用到代码
python manage.py startapp login
修改settings.py中的认证项
AUTHENTICATION_BACKENDS=( 'login.auth.UsernamePasswordAuth', )
自定义认证类
在login app下创建auth.py文件,内容如下
#coding:utf-8 from django.contrib.auth.models import User class UsernamePasswordAuth(object): def authenticate(self, username=None, password=None): print("UsernamePasswordAuth.authenticate") try: user=User.objects.get(username__iexact=username) if user.check_password(password): return user except User.DoesNotExist: return None def get_user(self, user_id): print("UsernamePasswordAuth.get_user") try: user=User.objects.get(pk=user_id) return user except User.DoesNotExist: return None
共0条 [查看全部]相关评论