[英]How Can I implement custom authentication in django for user login
I want to implement custom login authentication in Django. 我想在Django中实现自定义登录身份验证。 I do not want to use Django's inbuilt authentication system because it is not working.
我不想使用Django的内置身份验证系统,因为它无法正常工作。 I have a model named Doctor and I want to fetch the data from the Doctor model for login authentication.
我有一个名为Doctor的模型,我想从Doctor模型中获取数据以进行登录身份验证。 I have set my default database as MySQl database.
我已将默认数据库设置为MySQl数据库。
I have tried some code but it didn't work for me. 我已经尝试了一些代码,但是对我来说不起作用。
views.py views.py
def login(request):
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request,user)
return render(request,'welcome1.html') #success
else: #Invalid login
return render(request, 'login.html',{
'error_message':'Invalid Credential'
})
return render(request, 'login.html')
models.py models.py
class Doctor(models.Model):
initial_name = models.CharField(max_length=8)
first_name = models.CharField(max_length=20, unique=True)
last_name = models.CharField(max_length=20, unique=True)
hospital_name = models.CharField(max_length=20)
username = models.EmailField(max_length=50)
password = models.CharField(max_length=50)
address = models.CharField(max_length=1024)
zip_code = models.CharField(max_length=12)
dob = models.DateField(max_length=8)
gender = models.CharField(max_length=10)
phone_number = models.TextField(max_length=10)
def __str__(self):
return self.initial_name
My expected output should be "when I will put wrong Email & password, it will show an error" But it is automatically redirecting to my home page, if i put wrong eail or password. 我的预期输出应该是“当我输入错误的电子邮件和密码时,它将显示错误”,但是如果我输入错误的eail或密码,它将自动重定向到我的主页。
You hand better extend standard user model via AbstractUser 您最好通过AbstractUser扩展标准用户模型
https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project https://docs.djangoproject.com/zh-CN/2.2/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project
if you want to have more specific fields such as hospital_name , etc in your user model 如果您希望在用户模型中包含更具体的字段,例如hospital_name等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.