为什么身份验证不适用于自定义模型"顾问"。我已经尝试了很多,但它不起作用 models.py
class Consultants(models.Model):
# user=models.OneToOneField(User)
consul_id=models.IntegerField(default=0,primary_key=True)
first_name=models.CharField(max_length=255,blank=True,null=True)
last_name=models.CharField(max_length=255,blank=True,null=True)
email=models.EmailField(max_length=255,blank=True,null=True)
username=models.CharField(max_length=255,blank=True,null=True)
password=models.CharField(max_length=50,blank=True,null=True)
consul_pic=models.ImageField(upload_to="/home/cp/Documents/consul_pic",blank=True,null=True)
mobile_no=models.CharField(max_length=255,blank=True,null=True)
)
last_login=models.DateTimeField(default=datetime.now,blank=True,null=True)
is_active=models.BooleanField(default=False)
def __str__(self):
return self.first_name or u''
views.py
def login_user(request):
context = RequestContext(request)
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
print type(username)
try:
user = authenticate(username=username, password=password)
print 'chala'
if user.is_active:
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)
return HttpResponse("welcome......you are succesfuly log in")
else:
return HttpResponse("Your account is disabled.")
except ObjectDoesNotExist:
return HttpResponse("INvalid User")
else:
return render_to_response('login.html', {}, context)
当我尝试验证它时返回None。 这是我尝试登录的正确方法,请帮助我。
答案 0 :(得分:1)
这不是自定义用户模型的正确方法
你应该这样试试
参考:https://docs.djangoproject.com/en/1.9/topics/auth/customizing/
from django.contrib.auth.models import (AbstractBaseUser,
PermissionsMixin,
UserManager)
class Consultants(AbstractBaseUser, PermissionsMixin):
consul_id=models.IntegerField(default=0,primary_key=True)
first_name=models.CharField(max_length=255,blank=True,null=True)
last_name=models.CharField(max_length=255,blank=True,null=True)
email=models.EmailField(max_length=255,blank=True,null=True)
username=models.CharField(max_length=255,blank=True,null=True)
password=models.CharField(max_length=50,blank=True,null=True)
consul_pic=models.ImageField(upload_to="/home/cp/Documents/consul_pic",blank=True,null=True)
mobile_no=models.CharField(max_length=255,blank=True,null=True))
last_login=models.DateTimeField(default=datetime.now,blank=True,null=True)
is_active=models.BooleanField(default=False)
objects = UserManager()
def __str__(self):
return self.first_name or u''