我創建了一個用戶模型爲我的Django應用程序「用戶」對象沒有attribude is_authenticated
class User(Model):
"""
The Authentication model. This contains the user type. Both Customer and
Business models refer back to this model.
"""
email = EmailField(unique=True)
name = CharField(max_length=50)
passwd = CharField(max_length=76)
user_type = CharField(max_length=10, choices=USER_TYPES)
created_on = DateTimeField(auto_now_add=True)
last_login = DateTimeField(auto_now=True)
def __unicode__(self):
return self.email
def save(self, *args, **kw):
# If this is a new account then encrypt the password.
# Lets not re-encrypt it everytime we save.
if not self.created_on:
self.passwd = sha256_crypt.encrypt(self.passwd)
super(User, self).save(*args, **kw)
我還創建了一個驗證的中間件使用這種模式。
from accounts.models import User
from passlib.hash import sha256_crypt
class WaitformeAuthBackend(object):
"""
Authentication backend fo waitforme
"""
def authenticate(self, email=None, password=None):
print 'authenticating : ', email
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
user = None
if user and sha256_crypt.verify(password, user.passwd):
return user
else:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
我已經正確ammended settings.py文件,如果我一些打印語句添加到這個後端我可以看到用戶的詳細信息打印出來。我不記得我需要在django文檔中實現is_authenticated。我錯過了一些愚蠢的東西嗎?
我向用戶模型添加了一個is_authenticated方法,返回True。對我來說,它似乎愚蠢,但它似乎工作。用戶將永遠被認證? – nialloc