Django自定义User模型和登录验证
2013-06-02 15:40
716 查看
http://www.redicecn.com/html/blog/Django/2012/0325/385.html
用户表已存在(与其他App共用),不能再使用Django内置的User模型和默认的登录认证。但是还想使用Django的认证框架(真的很方便啊)。
两个步骤:
1)自定义Use模型,为了区分系统的User模型,命名为Account。
view plaincopy to clipboardprint?
class Account(models.Model):
business_email = models.EmailField()
business_password = models.CharField(max_length=20)
contact_first_name = models.CharField(max_length=30)
contact_last_name = models.CharField(max_length=30)
is_active = models.BooleanField()
def is_authenticated(self):
return True
def hashed_password(self, password=None):
if not password:
return self.business_password
else:
return hashlib.md5(password).hexdigest()
def check_password(self, password):
if self.hashed_password(password) == self.business_password:
return True
return False
class Meta:
db_table = "bussinesses"
2)自定义登录验证后台,并加入AUTHENTICATION_BACKENDS。
view plaincopy to clipboardprint?
# auth.py
from coocaca.myauth.models import Account
class MyCustomBackend:
def authenticate(self, business_email=None, business_password=None):
try:
user = Account.objects.get(business_email=business_email)
except Account.DoesNotExist:
pass
else:
if user.check_password(business_password):
return user
return None
def get_user(self, user_id):
try:
return Account.objects.get(pk=user_id)
except Account.DoesNotExist:
return None
在settings.py中加入该验证后台:
view plaincopy to clipboardprint?
AUTHENTICATION_BACKENDS = (
'coocaca.myauth.auth.MyCustomBackend',
)
这样Django就会使用MyCustomBackend作为登录验证后台。
验证通过后返回的是我们自定义的Account模型,并且request.user中获取的也是我们的Account模型(这正是MyCustomBackend中get_user方法的功能)。
用户表已存在(与其他App共用),不能再使用Django内置的User模型和默认的登录认证。但是还想使用Django的认证框架(真的很方便啊)。
两个步骤:
1)自定义Use模型,为了区分系统的User模型,命名为Account。
view plaincopy to clipboardprint?
class Account(models.Model):
business_email = models.EmailField()
business_password = models.CharField(max_length=20)
contact_first_name = models.CharField(max_length=30)
contact_last_name = models.CharField(max_length=30)
is_active = models.BooleanField()
def is_authenticated(self):
return True
def hashed_password(self, password=None):
if not password:
return self.business_password
else:
return hashlib.md5(password).hexdigest()
def check_password(self, password):
if self.hashed_password(password) == self.business_password:
return True
return False
class Meta:
db_table = "bussinesses"
2)自定义登录验证后台,并加入AUTHENTICATION_BACKENDS。
view plaincopy to clipboardprint?
# auth.py
from coocaca.myauth.models import Account
class MyCustomBackend:
def authenticate(self, business_email=None, business_password=None):
try:
user = Account.objects.get(business_email=business_email)
except Account.DoesNotExist:
pass
else:
if user.check_password(business_password):
return user
return None
def get_user(self, user_id):
try:
return Account.objects.get(pk=user_id)
except Account.DoesNotExist:
return None
在settings.py中加入该验证后台:
view plaincopy to clipboardprint?
AUTHENTICATION_BACKENDS = (
'coocaca.myauth.auth.MyCustomBackend',
)
这样Django就会使用MyCustomBackend作为登录验证后台。
验证通过后返回的是我们自定义的Account模型,并且request.user中获取的也是我们的Account模型(这正是MyCustomBackend中get_user方法的功能)。
相关文章推荐
- Django自定义User模型和登录验证
- Django学习系列之重写User模型和登录验证
- django 1.7 自定义用户user模型的三种方法
- 自定义Django中间件(登录验证中间件实例)
- Django学习笔记 — 自定义User模型
- django 1.7 自定义用户user模型的三种方法
- django 自定义用户user模型的三种方法
- django 自定义用户user模型的三种方法
- How to use the windows active directory to authenticate user via logon form 如何自定义权限系统,使用 active directory验证用户登录
- django 自定义登录验证逻辑
- django 自定义用户user模型的三种方法
- 基于 Django1.5自定义user模型
- Django(5) session登录注销、csrf及中间件自定义、django Form表单验证(非常好用)
- 使用MVC模型的用户登录及注册(验证用户是否存在)
- 自定义Django 带row caching功能的模型管理器
- 记录替换django的user模型出现的异常InconsistentMigrationHistory
- hive2 自定义验证登录
- Django学习笔记4 用户注册和身份验证、登录、注销、注册
- django 注册、登录及第三方接口程序(3):验证之正则表达式
- Django笔记---用户注册和登录验证(1)--Post请求