Django Authenticate方法不适用于自定义模型 [英] Django Authenticate method not working on custom Model

查看:43
本文介绍了Django Authenticate方法不适用于自定义模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用内置方法authenticate()来登录员工,但这似乎不起作用.我没有使用django提供的默认模型User,而是在这里使用了自己的模型Employee.

I am trying the build in method authenticate() to login employees but that does not seem to work. I have not used the default model User provided by django instead I have used my own model Employee here.

这就是我的观点.py

def login(request):
    if request.method == 'POST':
        password = request.POST['password']
        emailid = request.POST['email']

        employee = auth.authenticate(username=emailid,password=password)

        if employee is not None:
            auth.login(request,employee)
            return render(request,'register/html')
        
            
                
        else:
            messages.error(request,'Email id dosent exist')
            return redirect('login')
            
    else:
        return render(request,'employee/login.html')  

我的自定义代码进行身份验证

while my custom code does the authentication

def login(request):
    if request.method == 'POST':
        password = request.POST['password']
        emailid = request.POST['username']

         if Employee.objects.filter(firstname=emailid).exists():
            if Employee.objects.filter(password=password):
                messages.success(request,'Loged Out')
                return redirect('vacant-rooms')
            else:
                messages.error(request,'Wrong emailid')
                return redirect('login')
        else:
            messages.error(request,'wrong password')
            return redirect('login')
     else:
        return render(request,'employee/login.html')

这是我的模特

class Employee(models.Model):
    firstname = models.CharField(max_length=15)
    lastname = models.CharField(max_length=15)
    age = models.IntegerField()
    sex = models.CharField(max_length=10)
    phoneno = models.IntegerField()
    emailid = models.CharField(max_length=25)
    address = models.CharField(max_length=50)
    salary = models.IntegerField()
    designation = models.CharField(max_length=10)
    password = models.CharField(max_length=10)
    aadharno = models.CharField(max_length=15)
    datejoined = models.DateField(default=timezone)

这可能是一个愚蠢的问题,但是由于我的模型中没有用户名字段,所以身份验证中的问题不起作用吗?

Its a dumb question maybe but is the buit in authentication not working because I do not have a username field in my model?

推荐答案

我认为您不应该这样做.Django已经具有用户基础结构,并且要使用身份验证之类的功能,您需要继承AbstractUser或AbstractBaseUser类之一.如果只定义这样的模型,则必须完成Django已经完成的许多工作,我绝对不建议这样做.我在下面留下了一些有用的文档和文章.如果您需要任何帮助,请发表评论,我会尽力提供帮助.

I think you shouldn't do that. Django already has a User infrastructure and to use functions like authenticate, you need to inherit one of the AbstractUser or AbstractBaseUser classes. If you just define models like that, you have to do a lot of the work that Django has already done, and I definitely don't recommend it. I leave some helpful documentation and articles below. If you need any help, write a comment and I can help as best I can.

官方Django Doc自定义身份验证

自定义用户模型

如何扩展用户模型

这篇关于Django Authenticate方法不适用于自定义模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆