1
我正在使用Django 1.5。我是一個自定義的用戶模型是這樣的:即使密碼正確,check_password始終返回false?
class User(AbstractBaseUser):
#id = models.IntegerField(primary_key=True)
#identifier = models.CharField(max_length=40, unique=True, db_index=True)
username = models.CharField(max_length=90, unique=True, db_index=True)
create_time = models.DateTimeField(null=True, blank=True)
update_time = models.DateTimeField(null=True, blank=True)
email = models.CharField(max_length=225)
#password = models.CharField(max_length=120)
external = models.IntegerField(null=True, blank=True)
deleted = models.IntegerField(null=True, blank=True)
purged = models.IntegerField(null=True, blank=True)
form_values_id = models.IntegerField(null=True, blank=True)
disk_usage = models.DecimalField(null=True, max_digits=16, decimal_places=0, blank=True)
objects = UserManager()
USERNAME_FIELD = 'email'
class Meta:
db_table = u'galaxy_user'
我有一個自定義的驗證:
class AuthBackend:
def authenticate(self, username=None, password=None):
if '@' in username:
kwargs = {'email': username}
else:
kwargs = {'username': username}
try:
user = User.objects.get(**kwargs)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
即使輸入正確的用戶名和密碼check_password()總是返回false,所以我無法登錄後。我想,在終端也:
user.check_password(password)
總是返回False.
#views.py:
def login_backend(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
state = "Username or Password Incorrect!"
if user is not None:
login(request, user)
return HttpResponseRedirect('/overview/')
else:
return render_to_response('login_backend.html', {'state':state}, context_instance=RequestContext(request))
else:
return render_to_response('login_backend.html', context_instance=RequestContext(request))
是啊看起來是這樣的:f8566297ee28e8a3096497070b37b91d24c2在數據庫中。 – pynovice 2013-03-20 06:29:20
是否這樣:用戶返回user = User.objects.get(** kwargs)返回用戶名,但在我做user.check_password(密碼)它返回false。基本上它是對用戶名進行身份驗證,而不是用密碼進行身份驗證。 – pynovice 2013-03-20 06:41:42
好的。你稍等一會兒。 – pynovice 2013-03-20 06:45:03